我一直在玩java中的selenium,我遇到了一个我无法弄清楚的问题。
在此页面上:http://www.legalcontracts.com/contracts/lease-agreement-form/?ldcn=menu+name
从第三页(地址页面)到第四页 我可以设置地址:
@FindBy(xpath="//div[@class=' qd spropertyAddress']/div[@class='ans']/input")
private WebElement address;
public void SetAddress(String addressName){
address.sendKeys(addressName);
}
但是当我尝试继续
时@FindBy(xpath="//a[contains(text(),'Continue')]")
private WebElement continuebutton;
continuebutton.click();
我得到org.openqa.selenium.ElementNotVisibleException
。 “继续”按钮与上一页的标识@FindBy
相同,因此我不确定是否与此有关。我尝试了许多不同的方法来尝试处理这个问题,但似乎唯一有效的方法是thread.sleep
,这不是处理这个问题的好方法。
我尝试过的事情:
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(continuebutton));
wait.until(ExpectedConditions.elementToBeClickable(continuebutton));
wait.until(ExpectedConditions.presenceOfElementLocated((By) continuebutton));
wait.until(ExpectedConditions.visibilityOfElementLocated((By) continuebutton));
public void waitForJQuery(WebDriver driver) {
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
JavascriptExecutor js = (JavascriptExecutor) d;
return (Boolean) js.executeScript("return !!window.jQuery && window.jQuery.active == 0");
}
});
}
public void waitForJavaScript(WebDriver driver){
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
}
}
);
public void checkPageIsReady(WebDriver driver) { JavascriptExecutor js = (JavascriptExecutor)driver; //Initially bellow given if condition will check ready state of page.
if (js.executeScript("return document.readyState").toString().equals("complete")){
System.out.println("Page Is loaded.");
return;
}
//This loop will rotate for 25 times to check If page Is ready after every 1 second.
//You can replace your value with 25 If you wants to Increase or decrease wait time.
for (int i=0; i<25; i++){
try {
Thread.sleep(1000); }
catch (InterruptedException e) {}
//To check page ready state.
if (js.executeScript("return document.readyState").toString().equals("complete")){
break; } }
}
我从上一页继续使用相同的继续,但是在不同的页面对象中:
@FindBy(xpath="//a[contains(text(),'Continue')]")
private WebElement continuebutton;
continuebutton.click();
selenium会将当前的继续按钮与前一个按钮混淆吗?
答案 0 :(得分:0)
使用以下代码:
driver.findElement(By.cssSelector("div.ans>input#ctl00_Content_ctrlQuestions_ctl194_ctl21_ctl06_ctl01_ctl01_ctl01_in")).click();
driver.findElement(By.cssSelector("div.ans>input#ctl00_Content_ctrlQuestions_ctl194_ctl21_ctl06_ctl01_ctl01_ctl01_in")).clear();
driver.findElement(By.cssSelector("div.ans>input#ctl00_Content_ctrlQuestions_ctl194_ctl21_ctl06_ctl01_ctl01_ctl01_in")).sendKeys("ur address");
Thread.sleep(2000);
//click on continue button
driver.findElement(By.cssSelector(" .largeProceedButton.continueButtonnContinueFirst.nContinue")).click();
使用cssSelector而不是上面提到的xpath点击“继续”按钮。