Java 如果Selenium驱动程序没有找到元素(If-else-If),则继续前进,但它抛出一个can';找不到元素错误
长话短说-我有一个没有ID的按钮,并且有一个复合类(因此selenium讨厌它/找不到它)。因此,我使用XPath选择器进行选择,效果非常好Java 如果Selenium驱动程序没有找到元素(If-else-If),则继续前进,但它抛出一个can';找不到元素错误,java,selenium,chromium,Java,Selenium,Chromium,长话短说-我有一个没有ID的按钮,并且有一个复合类(因此selenium讨厌它/找不到它)。因此,我使用XPath选择器进行选择,效果非常好 driver.findElement(By.xpath("//input[@value='Continue to Payment']")).click() 但是按钮会根据所使用的语言而变化 所以现在,我有 if (driver.findElement(By.xpath("//input[@value='Continue to Payment']")).i
driver.findElement(By.xpath("//input[@value='Continue to Payment']")).click()
但是按钮会根据所使用的语言而变化
所以现在,我有
if (driver.findElement(By.xpath("//input[@value='Continue to Payment']")).isDisplayed()){
driver.findElement(By.xpath("//input[@value='Continue to Payment']")).click();
}
else if (driver.findElement(By.xpath("//input[@value='Paiement']")).isDisplayed()){
driver.findElement(By.xpath("//input[@value='Paiement']")).click();
}
else if ( same thing as above but for another language)
但当Selenium在执行第一个if语句后出错时:
no such element: Unable to locate element:{"method":"xpath","selector":"//a[contains(text(),'Checkout')]"}
我知道元素不在那里。。所以我不希望它做任何事情&继续下一个if-else语句。
我错过了什么
try {
if (driver.findElement(By.xpath("//input[@value='Continue to Payment']")).isDisplayed()){
driver.findElement(By.xpath("//input[@value='Continue to Payment']")).click();
}
else if (driver.findElement(By.xpath("//input[@value='Paiement']")).isDisplayed()){
driver.findElement(By.xpath("//input[@value='Paiement']")).click();
}
else
System.out.println("Button not found");
} catch(NoSuchElementException | StaleElementReferenceException e) {
System.out.println("Impossible to click the pop-up. Reason: " + e.toString());
}
试试上面的解决方案,希望它对你有用。在您的示例中,为else if(driver.findelelement(By.xpath(“//input[@value='Paiement']).isDisplayed)编写了错误的代码。
试试上面的解决方案,希望它对你有用。在您的示例中,为
else if(driver.findelelement(By.xpath(“//input[@value='Paiement'])).isDisplayed)编写了错误的代码。
您可以使用单独的短方法来获得预期的结果并记录错误
public WebElement getElement(WebDriver driver, String XPATH, int timeoutInSeconds){
WebElement elem = null;
try{
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
elem = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(XPATH));
} catch (Exception e){
// log or print error.
}
return elem;
}
你可以这样称呼它
WebElement e = getElement(driver, "//input[@value='Continue to Payment']", 10);
if (e != null) {
e.click();
} else {
e = getElement(driver, "//input[@value='Paiement']", 5);
if (e != null) {
e.click();
} /// and so on....
}
通过这种方式,您可以调整每个元素的等待时间,并且在由于语言原因缺少任何一个元素时也不会出错。您可以使用单独的简短方法来获得预期结果并记录错误
public WebElement getElement(WebDriver driver, String XPATH, int timeoutInSeconds){
WebElement elem = null;
try{
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
elem = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(XPATH));
} catch (Exception e){
// log or print error.
}
return elem;
}
你可以这样称呼它
WebElement e = getElement(driver, "//input[@value='Continue to Payment']", 10);
if (e != null) {
e.click();
} else {
e = getElement(driver, "//input[@value='Paiement']", 5);
if (e != null) {
e.click();
} /// and so on....
}
通过这种方式,您可以调整每个元素的等待时间,并且如果由于语言原因缺少任何一个元素,也不会出错。我认为您缺少try/catch。您还可以使用findElements获取一个列表。如果列表大小>0,则您已找到它。。。。如下所示:wait=newwebdriverwait(driver,ec_Timeout);List元素=wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(your_xpath));如果(element.size()>0){…}(但您仍应使用try/catch…),等待将引发超时…共享页面的HTML,可能还有其他方法来定位输入。@pcalkins谢谢!!移动到第二个值非常有效!!假设它既不是1号也不是2号。捕获也会有例外。我会让它尝试{code A-不在那里}catch{other code-不在那里}尝试{code c}还是尝试{code A+code B]catch{code c}我想到的一件事是提前初始化列表。然后,如果最后列表大小>0,则单击第一个元素…(在找到或没有找到所有元素之后…)我想您也可以创建一个函数,在找到一个项目时返回列表。这样可以避免在已经找到的情况下检查更多语言。请尝试/捕获每个单独的findElements()调用…此外,如果元素不是通过javascript创建的,则不需要webdriverwait。我认为您缺少了try/catch。您也可以使用findElements获取列表。如果列表大小>0,则您已经找到了它…类似这样的内容:wait=new webdriverwait(driver,ec_Timeout);list element=wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(您的_-xpath));if(element.size()>0){……}(但仍应使用try/catch…)等待将引发超时…共享页面的HTML,可能还有其他方法来定位输入。@pcalkins谢谢!!移动到第二个值非常好!!假设它不是第一个值或第二个值。捕获也会有异常。我会让它尝试{code A-不在那里}捕获{其他代码-不在那里}尝试{code c}或者尝试{code A+code B]Catch{code C}我想到的一件事是提前初始化列表。如果列表末尾的大小>0,则单击第一个元素…(在找到或未找到所有元素之后…)我想您也可以创建一个函数,在找到一个项目时返回列表。如果已经找到,则可以避免检查更多语言。请尝试/捕获每个单独的findElements()调用……此外,如果元素不是通过javascript创建的,您将不需要webdriverwait。