我想从下拉菜单中选择该选项。我尝试了很多方法但是我失败了。
我试过了:
WebDriverWait wait = new WebDriverWait (driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("iMacs")));
waitForElementToBeDisplayed(driver.findElement(By.linkText("iMacs")), 200);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='iMacs']")));
这是我的代码:
WebElement element = driver.findElement(By.linkText("Product Category"));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
WebDriverWait wait = new WebDriverWait (driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
WebElement subElement = driver.findElement(By.linkText("iMacs"));
action.moveToElement(subElement);
action.click();
action.perform();
这是我的错误:
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.linkText: iMacs (tried for 5 second(s) with 500 MILLISECONDS interval)
答案 0 :(得分:0)
所以你将鼠标悬停在一些元素上,然后尝试点击特定的链接?你在用哪个司机?你真的看到这个菜单在悬停后正确呈现了吗?
您的代码在wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
尝试使用wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("iMacs")));
,然后查看它是否失败(以及在这种情况下抛出的异常)。
为什么使用Actions API执行点击?
答案 1 :(得分:0)
我尝试在Chrome中执行您的代码,它始终适合我。但是,有时候,你失败了,你可以在你的代码中加入恢复机制,如下所示:
WebElement element = driver.findElement(By.linkText("Product Category"));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
WebDriverWait wait = new WebDriverWait (driver, 5);
try {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
} catch (WebDriverException we) {
System.out.println("First attempt to wait for visibility of 'iMacs' failed. Retrying...");
action.moveToElement(driver.findElement(By.linkText("Product Category"))).perform();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("iMacs")));
}
WebElement subElement = driver.findElement(By.linkText("iMacs"));
action.moveToElement(subElement);
action.click();
action.perform();
以上代码应始终有效。如果您有任何疑问,请与我们联系。
答案 2 :(得分:0)
请同时查看<select id="cost1" class="cost-block">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<select id="cost2" class="cost-block">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<select id="cost3" class="cost-block">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<button id='btn'>Send</button>
选项。可能是您的代码中的这个帮助。
sendkeys