简体   繁体   中英

Selenium Webdriver code works on debug but noy on a normal run

I'm trying to select an item on a listbox. The following code works when debugging the application but not on a normal run (as JUnit test)

wait.until(ExpectedConditions.elementToBeClickable(
  By.xpath("//div[@id='ContentArea']//tbody/tr[2]/td/div/span/span/span[2]/span")
));
driver.findElement(
  By.xpath("//div[@id='ContentArea']//tbody/tr[2]/td/div/span/span/span[2]/span")
).click();

wait.until(ExpectedConditions.elementToBeClickable(
  By.xpath("//ul[@id='symptomHeadGroupDropDown_listbox']/li[5]")
));
driver.findElement(
  By.xpath("//ul[@id='symptomHeadGroupDropDown_listbox']/li[5]")
).click();

Any ideas?

Try this code, it will wait for each second till 60 seeconds to find and click element:

int flag=0,wait=0;
while(flag==0 && wait<60){
    try{
        driver.findElement(By.xpath("//div[@id='ContentArea']/div/div/div[2]/div/table/tbody/tr[2]/td/div/span/span/span[2]/span")).click(); 
        flag=1;
    }
    catch(Exception){
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        wait++;
    }
 }

flag=0,wait=0;  

while(flag==0 && wait<60){
    try{
                driver.findElement(By.xpath("//ul[@id='symptomHeadGroupDropDown_listbox']/li[5]")).click(); 
        flag=1;
    }
    catch(Exception){
        driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        wait++;
    }
} 

My theory would be that the state of the WebElement is changing between your wait.until call and the second time you resolve it to invoke click.

Rather than resolving the WebElement multiple times, call click() on the WebElement return value from your call to the WebDriverWait .

WebElement target1 = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='ContentArea']//tbody/tr[2]/td/div/span/span/span[2]/span")));
target1.click();

WebElement target2 =wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@id='symptomHeadGroupDropDown_listbox']/li[5]")));
target2.click();

Or, if you don't want to store it as a temp var...

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='ContentArea']//tbody/tr[2]/td/div/span/span/span[2]/span"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@id='symptomHeadGroupDropDown_listbox']/li[5]"))).click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM