selenium 课题->自动登录签到并且发表评论 什么值得买

   java版本

    

package com.duanpeng.test.version1;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
         WebDriver driver = new ChromeDriver();
         driver.get("http://www.smzdm.com");
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
         driver.findElement(By.id("navBar_login")).click();
         driver.findElement(By.id("user_login")).sendKeys("username");
         driver.findElement(By.id("user_pass")).sendKeys("password");
         driver.findElement(By.id("btn_login")).click();
         
         
         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
             public Boolean apply(WebDriver d) {
                 return d.findElement(By.id("user_info_tosign")).getText().equals("已签到3天");
             }
         });
         
         WebElement element = driver.findElement(By.id("user_info_tosign"));
         
         if(element.getText().contains("已签到")){
        	 System.out.println(element.getText());
         }else{
        	 //触发按钮事件
        	 
         }
         
         WebElement el = driver.findElements(By.className("picLeft")).get(3);
         el.click();
         System.out.println(Arrays.toString(driver.getWindowHandles().toArray()));
         driver.switchTo().window((String)(driver.getWindowHandles().toArray()[1]));
         System.out.println("456:");
         System.out.println("123:"+driver.getTitle());
         driver.findElement(By.id("textareaComment")).click();
         driver.findElement(By.id("textareaComment")).sendKeys("漂亮,就是有点贵啊,叫我屌丝情何以堪!!!");
         driver.findElement(By.id("textCommentSubmit")).click();
         
         System.out.println(driver.getPageSource());
         
         
         try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         driver.close();
         
       
    }
}

  python 版本

 

#coding=utf-8
__author__ = 'duanpeng'


from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0

import  time
import  sys

print sys.maxunicode
username = "[email protected]"
password = "123456"

#开启浏览器驱动
driver = webdriver.Chrome()
driver.implicitly_wait(10)
#打开网址
driver.get("http://www.smzdm.com/")
#窗口最大化
driver.maximize_window()
#寻找登录按钮并且点击
driver.find_element_by_id('navBar_login').click()
#输入用户名
driver.find_element_by_id('user_login').send_keys(username)
#输入密码
driver.find_element_by_id('user_pass').send_keys(password)
#点击登录
driver.find_element_by_id('btn_login').click()
#time.sleep(5)


print 'start wait'
WebDriverWait(driver,3).until(lambda driver : driver.find_element_by_id('navBar_login_Info').is_displayed()==True)
print  'end wait'
webElement = driver.find_element_by_id('user_info_tosign')
if(webElement.text ==u'签到领积分'):
    webElement.click()
else:
    print webElement.text

#获取首页的第一条产品
webElement = driver.find_elements_by_class_name('picLeft')[0]
webElement.click()
driver.switch_to.window(driver.window_handles[1])

#睡眠30秒,等待页面AJAX加载完毕
time.sleep(30)
print driver.find_element_by_id('comment_avatar').get_attribute('alt')
#发表评论
WebDriverWait(driver,10).until(lambda driver : driver.find_element_by_id('comment_avatar').get_attribute('alt')==u'silencegg')
driver.find_element_by_id('textareaComment').click()
driver.find_element_by_id('textareaComment').send_keys(u"beautiful! i like it!")
driver.find_element_by_id('textCommentSubmit').click()
time.sleep(5)

#退出驱动
driver.quit()






你可能感兴趣的:(selenium 课题->自动登录签到并且发表评论 什么值得买)