【问题标题】:Selenium how can I click each identical button in a list and submit a formSelenium 如何单击列表中的每个相同按钮并提交表单
【发布时间】:2020-07-06 01:19:54
【问题描述】:

有问题的网址https://www.doctorxdentist.com/find-a-doctor

我希望执行以下步骤

  1. 过滤器列表 1.1) 点击医学区域 1.2) 点击牙科 1.3)点击专业 1.4) 点击牙医
  2. 点击获取报价按钮
  3. 填写表格
  4. 重复但点击下一个获取报价按钮

到目前为止,这是我的代码。我希望这些按钮不是唯一的,我可以使用 xpath 的减量来获取每个获取报价按钮,但不幸的是它们的 xpath 都是一样的。

P.S 似乎我最初的点击语句没有按预期工作。如果您可以检查它们,那就太好了

    package Script;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class DoctorXDentist {

    public static boolean isClickable(WebElement webe, WebDriver browser){
        try
        { 
        WebDriverWait wait = new WebDriverWait(browser, 5);
        wait.until(ExpectedConditions.elementToBeClickable(webe));
           return true;
         }
        catch (Exception e)
        {
        return false;
         }
    }


    
    public static void initiate() {
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\jinyi\\Downloads\\chromedriver_win32 (2)\\chromedriver.exe");
            /*
             * ChromeOptions options = new ChromeOptions();
             * options.setHeadless(true);
             */
            WebDriver browser = new ChromeDriver();
            browser.manage().window().maximize();
            browser.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
            browser.get("https://www.doctorxdentist.com/find-a-doctor");

            
            Actions action = new Actions(browser);
            // xpth -> go to clickable, copy xpath
            // Click Area of Medicine
            action.moveToElement(browser.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div/div[2]/div/form/div[4]/div/div[1]/div"))).sendKeys(Keys.RETURN);
            WebElement element = browser.findElement(By.xpath("//*[@id=\\\"app\\\"]/div/div[1]/div/div[2]/div/form/div[4]/div/div[1]/div"));  
            isClickable(element, browser);
            // Click Dental
            action.moveToElement(browser.findElement(By.xpath("//*[@id=\"select\"]/div/div[6]/label/span[2]"))).click();
            // Click specialty
            action.moveToElement(browser.findElement(By.xpath("//*[@id=\"app\"]/div/div[1]/div/div[2]/div/form/div[3]/div/div[1]/div"))).click();
            // Click Dentist
            action.moveToElement(browser.findElement(By.xpath("//*[@id=\"select\"]/div/div[3]/label/span[2]"))).click();
            List<WebElement> box = null; 
            for ( int i = 1; i < 200; i++) {
            box.add((WebElement) browser.findElements(By.xpath("//*[@class='box'][i]//*[@class='doctor']//button[text()='Get Quote']")));
            System.out.println(box);
            }
            // after get quote function completed
            
        // click on form
        //  action.moveToElement(browser.findElement(By.xpath("/html/body/div[5]/div[2]/div/section/div/div[2]/section/div[1]/div[1]/div/input"))).click();
    }
    public static void main(String[] args) {
        initiate();

    }

}

我也收到了以下消息,它没有说任何错误,我不确定这是什么意思

    Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 83.0.4103.116, chrome: {chromedriverVersion: 83.0.4103.39 (ccbf011cb2d2b..., userDataDir: C:\Users\jinyi\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:59331}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: 12f47c7e9735f47822adfbedf2dd3619
*** Element info: {Using=xpath, value=//*[@id=\"app\"]/div/div[1]/div/div[2]/div/form/div[4]/div/div[1]/div}
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:196)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:129)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:161)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:582)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:333)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:451)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:394)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:325)
    at Script.DoctorXDentist.initiate(DoctorXDentist.java:49)
    at Script.DoctorXDentist.main(DoctorXDentist.java:68)

【问题讨论】:

  • 嗨,你能帮帮我吗,你指的是哪个按钮-“不幸的是,他们的 xpath 都是一样的”
  • @DurgaPrasadBehera 这是获取报价按钮
  • 试试这个 - //*[@class='doctor']//button[text()='Get Quote']
  • 我可以得到按钮,但我需要它在提交每个表单后点击下面的按钮,你也可以检查我之前的点击语句是否有效?感谢您的帮助!
  • 这是给出唯一数据- //*[@class='box'][1]//*[@class='doctor']//button[text()='Get Quote ']

标签: java selenium web-scraping


【解决方案1】:

这段代码对我来说很好用。如果这有帮助,请标记为答案。 优化代码。

WebDriverWait wait = new WebDriverWait(driver, 60);
JavascriptExecutor js = (JavascriptExecutor) driver;
        
    try {
         driver.get("https://www.doctorxdentist.com/find-a-doctor");
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
         driver.manage().window().maximize();
            
    // Click Area of Medicine
     wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//div[@class='collapse form-box']//b[text()='Area of Medicine']"))));
     driver.findElement(By.xpath("//div[@class='collapse form-box']//b[text()='Area of Medicine']")).click();

    // Click Dental
     driver.findElement(By.xpath("//div[@id='select']//span[contains(text(),' Dental ')]")).click();

    // Click Speciality
     driver.findElement(By.xpath("//div[@class='collapse form-box']//b[text()='Speciality']")).click();

    // Click Dentist
     driver.findElement(By.xpath("//div[@id='select']//span[contains(text(),'Dentist')]")).click();

    // Click on Submit
     driver.findElement(By.xpath("//button[@type='submit']")).click();
            
     wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//*[@class='box'][1]//*[@class='doctor']//button[text()='Get Quote']"))));
            
     int size = driver.findElements(By.xpath("//*[@class='box']//*[@class='doctor']//button[text()='Get Quote']")).size();
            
     IntStream.range(1, size).forEach($ -> {
           js.executeScript("window.scrollTo(document.body.scrollHeight,0)");
           driver.findElement(By.xpath("//*[@class='box']["+$+"]//*[@class='doctor']//button[text()='Get Quote']")).click();
           driver.findElement(By.xpath("//button[text()='Cancel']")).click();
            });
            
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            driver.quit();
        }

【讨论】:

  • 您能否告知您是如何获得 xpath 的?我确实检查了元素副本 xpath,我总是得到这些/html/body/div[5]/div[2]/div/section/div/div[2]/section/div[1]/div[2] /div/输入
  • 我创建的这些xpaths,可能你可以通过教程或者你可以查看ChroPath之类的工具:)
  • 你的意思是你手动写的?感谢您介绍 xpath,我现在正在使用它,但我看到您的 xpath 不同
  • 是的,我手动编写的。这取决于你如何编写,XPath 有不同的功能和轴。
  • 特长比较棘手,chrompath的xpath不起作用
猜你喜欢
  • 2018-03-25
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 2022-01-07
  • 1970-01-01
相关资源
最近更新 更多