Java 为什么我的WebDriver等待方法没有持续地点击按钮?
为什么我的WebDriver等待方法没有持续地点击按钮 例如,在每100个测试中,有7个测试会失败,说定位器不可见,但当查看屏幕截图时,按钮明显在那里Java 为什么我的WebDriver等待方法没有持续地点击按钮?,java,selenium,selenium-webdriver,webdriver,Java,Selenium,Selenium Webdriver,Webdriver,为什么我的WebDriver等待方法没有持续地点击按钮 例如,在每100个测试中,有7个测试会失败,说定位器不可见,但当查看屏幕截图时,按钮明显在那里 <input class="btn btn-default dropdown-toggle buynow" aria-expanded="false" value="Buy Now" type="submit"/> 我尝试过等待、正常点击、循环和JS点击等,但没有持续点击按钮 public void waitAndCli
<input class="btn btn-default dropdown-toggle buynow" aria-expanded="false" value="Buy Now" type="submit"/>
我尝试过等待、正常点击、循环和JS点击等,但没有持续点击按钮
public void waitAndClickElement(WebElement element) throws InterruptedException {
boolean clicked = false;
int attempts = 0;
while (!clicked && attempts < 1000) {
try {
wait.until(ExpectedConditions.elementToBeClickable(element)).click();
System.out.println("Successfully clicked on the WebElement: " + element.toString());
clicked = true;
} catch (Exception e) {
System.out.println("Unable to wait and click on WebElement" + element + ", Exception: " + e.getMessage());
Assert.fail("Method failed: waitAndClickElement");
//Assert.fail("Unable to wait and click on the WebElement, using locator: " + element.toString());
}
attempts++;
}
}
public void clickOnBuyNowButton() throws InterruptedException {
WebElement buyNowButton = driver.findElement(By.xpath("html/body/div[1]/div[3]/div[1]/div/div/section/div[2]/div[2]/div[2]/form/div/input"));
WaitUntilWebElementIsVisible(buyNowButton);
Actions action = new Actions(driver);
action.moveToElement(buyNowButton).doubleClick().build().perform();
}
public void waitAndClickElement(WebElement元素)抛出中断异常{
布尔值=假;
int=0;
当(!单击并尝试<1000次时){
试一试{
等待.until(ExpectedConditions.elementToBeClickable(element))。单击();
System.out.println(“成功单击WebElement:+element.toString());
单击=真;
}捕获(例外e){
System.out.println(“无法等待并单击WebElement”+element+”,异常:+e.getMessage());
Assert.fail(“方法失败:waitAndClickElement”);
//Assert.fail(“无法等待并使用定位器单击WebElement:”+element.toString());
}
尝试++;
}
}
public void clickOnBuyNowButton()引发InterruptedException{
WebElement buyNowButton=driver.findElement(By.xpath(“html/body/div[1]/div[3]/div[1]/div/div/div/section/div[2]/div[2]/form/div/input”);
WaitUntilWebElementsVisible(立即购买按钮);
动作动作=新动作(驱动);
action.moveToElement(buyNowButton).双击().build().perform();
}
有什么想法吗?我做错什么了吗
感谢您的帮助尝试以下内容;在这里,我首先等待一个元素可点击,然后一旦它成为可点击元素,我将移动到它,然后抛出我的点击:
WebDriverWait wait = new WebDriverWait (driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(yourElement));
Actions act = new Actions(driver);
act.moveToEelment(yourElement).click().build().perform();
@kushal.even that dosnt work:/it就好像即使设置了15秒的超时,它也不停地尝试单击元素当20秒过去时会发生什么?@kushal.会引发异常,例如无法单击元素或定位器不可见,但是我已经捕获了屏幕截图,按钮是selenium只与DOM一起工作,因此如果有元素存在,selenium就可以进行交互;不管怎样,你有没有试过JavascriptExecutor?行动课呢?是的,恐怕我有:/