20210707web 作业

本贴最后更新于 1229 天前,其中的信息可能已经水流花落
24 回帖
请输入回帖内容 ...
  • Morris
    from selenium import webdriver
    import time
    driver=webdriver.Chrome()
    driver.maximize_window()
    driver.get("https://www.baidu.com/")
    driver.find_element_by_id('kw').send_keys('测试派')
    driver.find_element_by_id('su').click()
    time.sleep(2)
    driver.find_element_by_xpath('//*[@id="1"]/h3/a').click()
    driver.implicitly_wait(10)
    driver.find_element_by_xpath('/html/body/div[1]/ul/li[2]/a').click()
    
  • mary369

    #coding:utf-8

    from selenium import webdriver
    from time import sleep

    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get('https://www.baidu.com')
    browser.implicitly_wait(5)

    browser.find_element_by_xpath('//input[@id="kw"]').send_keys(u'测试派')
    browser.find_element_by_xpath('//input[@id="su"]').click()
    browser.find_element_by_xpath('//*[@id="3"]/h3/a').click()

    handles = browser.window_handles
    browser.switch_to.window(handles[1])
    browser.find_element_by_xpath('//a[text()="领域"]').click()
    sleep(5)

    browser.quit()

  • H

    public ChromeDriver() {

    this(ChromeDriverService.createDefaultService(), new ChromeOptions());
    }

    public static final String CHROME_DRIVER_EXE_PROPERTY = "webdriver.chrome.driver";

    public static void main( String[] args )

    {

    System.setProperty("webdriver.chrome.driver","D:\test\driver\chromeDriver.exe");

    WebDriver driver = new ChromeDriver();

    driver.get("http://testingpai.com/");
    }
    driver.find_element_by_css_selector("fn__layer fn__layer--big").click()

  • ke2beck
    package com.demo;
    import org.openqa.selenium.chrome.ChromeDriver;
    import java.util.Set;
    
    public class demo {
        public static void main(String[] args) throws InterruptedException {
            System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
            ChromeDriver driver = new ChromeDriver();
            driver.get("https://www.baidu.com/");
            driver.findElementById("kw").sendKeys("测试派");
            driver.findElementById("su").click();
            Thread.sleep(1000);
            driver.findElementByPartialLinkText("软件测试工程师的心灵社区").click();
            Thread.sleep(1000);
            String search_handle = driver.getWindowHandle();
            Set<String> handles = driver.getWindowHandles();
            for (String handle : handles) {
                if (!handle.equals(search_handle)) {
                    driver.switchTo().window(handle);
                    Thread.sleep(1000);
                    driver.findElementById("navLogin").click();
                    Thread.sleep(1000);
                    driver.findElementById("nameOrEmail").sendKeys("ke2beck");
                    driver.findElementById("loginPassword").sendKeys("********");
                    driver.findElementById("loginBtn").click();
                    Thread.sleep(1000);
                    driver.findElementByXPath("/html/body/header/nav/a[2]").click();
                    Thread.sleep(5000);
                    driver.close();
                }
            }
            driver.quit();
        }
    }
    
    ```
    
    ```
    
    1 回复
  • xiaoyu9803

    B686480970AC4BAAA34308A2912BCC24.png

  • Tony
    public static void loginYouxuePai() throws InterruptedException {
            System.setProperty("webdriver.chrome.driver", "D:\\Program Files\\chromedriver.exe");
            ChromeDriver webDriver = new ChromeDriver();
    
            webDriver.get("https://www.baidu.com");
    
            WebElement kw = webDriver.findElement(By.id("kw"));
            kw.sendKeys("测试派");
    
            WebElement su = webDriver.findElement(By.id("su"));
    
            String windowHandle = webDriver.getWindowHandle();
            su.click();
    
            Thread.sleep(3000);
    
    
            WebElement element = webDriver.findElement(By.xpath("//em[text()=\"测试派\"]/parent::*"));
            element.click();
            Thread.sleep(3000);
    
            webDriver.switchTo().defaultContent();
            webDriver.manage().window().maximize();
            System.out.println("页面跳转成功");
    
            Set<String> windowHandles = webDriver.getWindowHandles();
            for (String s : windowHandles){
                Thread.sleep(1000);
                if (webDriver.switchTo().window(s).getTitle().equals("测试派 - 软件测试工程师的心灵社区")){
                    windowHandle = s;
                    break;
                }
            }
    
            webDriver.switchTo().window(windowHandle);
            Thread.sleep(1000);
            //element = webDriver.findElement(By.cssSelector(".fn__two-line"));
            element =  webDriver.findElement(By.xpath("//a[@href=\"http://testingpai.com/article/1625637760328/comment/1625650976958#comments\"]/div"));
            if (element != null){
                System.out.printf("找到元素了");
            }
    
            element.click();
        }
    
    1 操作
    Tony 在 2021-07-08 15:13:28 更新了该回帖
  • qingchen

    #导包
    from selenium import webdriver
    from time import sleep
    #创建一个驱动对象
    browser = webdriver.Chrome()
    #使用谷歌浏览器驱动打开浏览器
    browser.get('https://www.baidu.com')
    #设置抓取标签等待时间5秒
    browser.implicitly_wait(5)
    #通过id定位搜索框 填写测试派
    browser.find_element_by_id('kw"]').send_keys('测试派')
    #browser.find_element_by_xpath//input[@id='kw' and @name='wd']
    #通过xpah定位确定键 点击
    browser.find_element_by_xpath('//input[@id="su"]').click()
    #等待5秒
    time.sleep(5)

    #下面没网络了 视频看不成了 写不下去了

    #通过xpah定位测试派位置点击
    browser.find_element_by_xpath('//em[text()="测试派"]').click()

    browser.quit()

    1 回复
  • Noone

    package cn.com.test;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;

    import org.openqa.selenium.chrome.ChromeDriver;

    public class findElement {

    public static void main(String[] args) {

    ChromeDriver driver = new ChromeDriver();
    
    driver.get("http://www.baidu.com");
    
    driver.findElementByXPath("//input[@id='kw' and @name='wd']").sendKeys("测试派");
    
    driver.findElementByCssSelector("#su").click();
    
    try {
    	Thread.sleep(1000);
    } catch (InterruptedException e1) {
    	// TODO Auto-generated catch block
    	e1.printStackTrace();
    }
    
    driver.findElementByXPath("//em[text()=\"测试派\"]/parent::a").click();
    
    try {
    	Thread.sleep(1000);
    } catch (InterruptedException e1) {
    	// TODO Auto-generated catch block
    	e1.printStackTrace();
    }
    
    Set<String> winHandels=driver.getWindowHandles();// 得到当前窗口的set集合
    List<String> it = new ArrayList<String>(winHandels); // 将set集合存入list对象
    driver.switchTo().window(it.get(1));// 切换到弹出的新窗口
    
    
    driver.findElementById("navLogin").click();
    
    try {
    	Thread.sleep(1000);
    } catch (InterruptedException e1) {
    	// TODO Auto-generated catch block
    	e1.printStackTrace();
    }
    
    // 登录
    driver.findElementById("nameOrEmail").sendKeys("输入账号");
    
    driver.findElementById("loginPassword").sendKeys("输入密码");
    
    try {
    	Thread.sleep(1000);
    } catch (InterruptedException e1) {
    	// TODO Auto-generated catch block
    	e1.printStackTrace();
    }
    
    // 登录
    driver.findElementById("loginBtn").click();
    
    try {
    	Thread.sleep(1000);
    } catch (InterruptedException e1) {
    	// TODO Auto-generated catch block
    	e1.printStackTrace();
    }
    
    // 点击 ‘领域’标签    /html/body/header/nav/a[2]
    driver.findElementByXPath("/html/body/header/nav/a[2]").click();
    
    
    try {
    	Thread.sleep(5000);
    } catch (InterruptedException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    }
    
    driver.quit();
    

    }
    }

    1 回复
  • scb21sunny

    QQ截图20210708140039.png
    QQ截图20210708140049.png

    1 回复
  • luojie

    作业要求访问测试派

  • luojie

    满分

  • luojie

    继续加油

  • luojie

    满分

  • Tony
    public static void textControl(WebDriver driver, String name,String txt){
            WebElement element = driver.findElement(By.name(name));
            element.clear();
            element.sendKeys(txt);
        }
        public static void loginFeizhu() throws InterruptedException {
            WebDriver driver = init();
            driver.get("https://www.fliggy.com/");
    
            Thread.sleep(1000);
            /*
            //点击搜索框
            driver.findElement(By.xpath("//input[@placeholder=\"目的地/酒店/景点/签证等\"]")).click();
            Thread.sleep(1000);
            //选择机票
            driver.findElement(By.xpath("//div[@class=\"recommend-values\"]/span[contains(text(),\"机票\")and @data-itemindex=0]")).click();
            */
            textControl(driver,"depCityName", "上海"));
            textControl(driver,"arrCityName","长沙");
            textControl(driver,"depDate","2021-07-10");
    
            driver.findElement(By.cssSelector(".close-btn.delegate-click-381")).click();
            Thread.sleep(3000);
            driver.findElement(By.xpath("//form[@id=\"J_FlightForm\"]//button[text()=\"搜索\"]")).click();
    
            tearDown(driver);
        }
    
  • H

    public static void textControl(WebDriver driver, String name,String txt){ WebElement element = driver.findElement(By.name(name)); element.clear(); element.sendKeys(txt); } public static void loginFeizhu() throws InterruptedException { WebDriver driver = init(); driver.get("https://www.fliggy.com/"); Thread.sleep(1000); driver.findElement(By.xpath("//input[@placeholder="目的地/酒店/景点/签证等"]")).click(); Thread.sleep(1000); driver.findElement(By.xpath("//div[@class="recommend-values"]/span[contains(text(),"机票")and @data-itemindex=0]")).click(); */ textControl(driver,"depCityName", "上海")); textControl(driver,"arrCityName","长沙"); textControl(driver,"depDate","2021-07-10"); driver.findElement(By.cssSelector(".close-btn.delegate-click-381")).click(); Thread.sleep(3000); driver.findElement(By.xpath("//form[@id="J_FlightForm"]//button[text()="搜索"]")).click(); tearDown(driver); }

    1 回复
  • ke2beck
    package com.demo;
    
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class fliggyDemo {
      
        public static void inputElement(ChromeDriver driver, String eleName, String text) {
            WebElement element = driver.findElementByName(eleName);
            element.clear();
            element.sendKeys(text);
        }
        public static void main(String[] args) throws InterruptedException {
    
            System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
            ChromeDriver driver = new ChromeDriver();
    
            driver.get("https://www.fliggy.com/");
            Thread.sleep(2000);
            inputElement(driver,"depCityName","北京");
    
            inputElement(driver,"depDate","2021-07-10");
            driver.findElementByXPath("//span[text()='close']").click();
    
            inputElement(driver,"arrCityName","长沙");
            driver.findElementByXPath("//form[@id='J_FlightForm']//button").click();
    
            Thread.sleep(5000);
            driver.quit();
        }
    }
    
    
    1 回复
  • qingchen

    #导包
    from selenium import webdriver
    from time import sleep
    #创建一个驱动对象
    browser = webdriver.Chrome()
    #使用谷歌浏览器驱动打开浏览器飞猪网主页
    browser.get('https://www.fliggy.com/')

    #通过名字定位出发城市填写框
    DepCityName = browser.find_element_by_name('depCityName')
    #清空出发城市
    DepCityName.clean()
    #出发城市填入北京
    DepCityName.send_keys('北京')
    #模拟人休息2秒
    time.sleep(2)

    #通过名字定位出发时间填写框
    DepDate = brower.find_element_by_name('depDate')
    #填写框填写2021-7-9
    DepDate.send_keys('2021-7-9')
    #模拟人休息2秒
    time.sleep(3)
    #点击关闭日期控件
    browser.find_element_by_Xpath('//span[text()='close']').click()
    #模拟人休息2秒
    time.sleep(2)

    #通过名字定位到达城市填写框
    ArrCityName = browser.find_element_by_name('arrCityName')
    #到达城市填入长沙
    ArrCityName.send_keys('长沙')
    #模拟人休息2秒
    time.sleep(2)

    #通过名字定位找到搜索按钮
    SearchButtom = browser.find_element_by_Xpath('//form[@id='J_FlightForm']//button')
    #点击搜索按钮
    SearchButtom.click()
    #模拟人休息2秒
    time.sleep(2)
    browser.quit()

    1 回复
  • mary369

    #coding:utf-8

    from selenium import webdriver
    from time import sleep

    #初始化
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get('https://www.fliggy.com/')
    browser.implicitly_wait(10)

    #输入出发城市
    depCity = browser.find_element_by_xpath('//input[@name="depCityName"]')
    depCity.clear()
    depCity.send_keys(u'昆明')
    sleep(1)
    #输入到达城市
    arrCity = browser.find_element_by_xpath('//input[@name="arrCityName"]')
    arrCity.clear()
    arrCity.send_keys(u'长沙')
    sleep(1)
    #输入出发日期
    depDate = browser.find_element_by_xpath('//input[@name="depDate"]')
    depDate.send_keys('2021-07-10')
    #关闭日期控件
    browser.find_element_by_xpath('//span[text()="close"]').click()
    sleep(2)
    #点击搜索按钮
    browser.find_element_by_xpath('//form[@id="J_FlightForm"]//button[text()="搜索"]').click()
    sleep(5)

    #退出
    browser.quit()

    1 回复
  • luojie

    满分

  • luojie

    满分

  • luojie

    满分

  • luojie

    代码没问题,格式化一下就更好了

  • scb21sunny

    QQ截图20210709150132.png
    QQ截图20210709150149.png
    QQ截图20210709150142.png
    QQ截图20210709150204.png
    QQ截图20210709150157.png

    1 回复
  • luojie

    666 满分~

请输入回帖内容 ...