Fenzox
Fenzox

Reputation: 354

Selenium how can I click each identical button in a list and submit a form

Url in question https://www.doctorxdentist.com/find-a-doctor

I wish to do the following steps

  1. Filter list 1.1) click area of medicine 1.2) click dental 1.3) click specialty 1.4) click dentist
  2. Click on the get quote button
  3. Fill form
  4. repeat but click on the next get quote button

This is my code so far. I was hoping the buttons are not unique in a way that I can use decrement for the xpath to get each get quote button but unfortunately their xpath is all the same.

P.S It seems that my initial clicking statements don't work as desired. If you can check them, that will be great

    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();

    }

}

I'm getting the follow message as well, it doesn't say any error and I'm not sure what it means

    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)

Upvotes: 1

Views: 586

Answers (1)

Durga Prasad Behera
Durga Prasad Behera

Reputation: 337

This code works fine for me. If this helps, please mark as answer. Optimize the code.

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();
        }

Upvotes: 1

Related Questions