Java 预期条件失败:等待 By.xpath 定位的元素的可见性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44920201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expected condition failed: waiting for visibility of element located by By.xpath
提问by user3030202
I am trying to click on Sign in link on site alibaba.com
我正在尝试点击 alibaba.com 网站上的登录链接
This is my test case:
这是我的测试用例:
public class TestCase {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
String URL = "http://www.alibaba.com/";
WebDriver driver;
System.setProperty("webdriver.chrome.driver",
"D:\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
driver.get(URL);
Thread.sleep(2000);
SignIn.SignIn_click(driver).click();
}
}
This is object class where in am locating the web element
这是我定位 web 元素的对象类
package PageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SignIn {
private static WebElement element = null;
public static WebElement SignIn_click(WebDriver driver) {
element = (new WebDriverWait(driver, 10)).until(ExpectedConditions
.visibilityOfElementLocated(By
.xpath("//a[@data-val='ma_signin']")));
element = driver.findElement(By
.xpath("//a[@data-val='ma_signin']"));
return element;
}
}
But when I run this code , I'm always getting this exception:
但是当我运行这段代码时,我总是遇到这个异常:
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //a[@data-val='ma_signin'] (tried for 10 second(s) with 500 MILLISECONDS interval)
Build info: version: 'unknown', revision: '86a5d70', time: '2017-02-16 07:47:51 -0800'
System info: host: 'ANUM-PC', ip: '172.16.11.162', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.30.477700 (0057494ad8732195794a7b32078424f92a5fce41), userDataDir=C:\Users\Anum\AppData\Local\Temp\scoped_dir1716_14873}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=59.0.3071.115, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: d0c1083c113270bd4ded08846544878e
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:257)
at PageObjects.SignIn.SignIn_click(SignIn.java:15)
at AutomationFramework.TestCase.main(TestCase.java:24)
Please help me on this.
请帮我解决这个问题。
采纳答案by Sudha Velan
Please try this following:
请尝试以下操作:
package PageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SignIn {
private static WebElement element = null;
public static WebElement SignIn_click(WebDriver driver) throws InterruptedException {
element = driver.findElement(By.xpath("id('J_SC_header')/header/div[2]//span[1]/a[@data-val='ma_signin']"));
while (!isDisplayed(element))
{
Thread.sleep(3000);
System.out.println("Element is not visible yet");
}
return element;
}
public static boolean isDisplayed(WebElement element) {
try {
if(element.isDisplayed())
return element.isDisplayed();
}catch (NoSuchElementException ex) {
return false;
}
return false;
}
}
回答by DebanjanB
Here is the Answer to your Question:
以下是您问题的答案:
The xpath
you have constructed //a[@data-val='ma_signin']
is not unique. The xpath
matches with 3 nodes. If you want to click on Sign In
button you can consider using this unique xpath
:
在xpath
你创建//a[@data-val='ma_signin']
不是唯一的。在xpath
与3个节点相匹配。如果你想点击Sign In
按钮,你可以考虑使用这个独特的xpath
:
//div[@id='J_SC_header']//div[@class='sc-hd-row sc-hd-main']//a[@rel='nofollow'][@data-val='ma_signin']
回答by Bhavesh Soni
public static void waitVisibilityOfElementLocated(WebDriver driver, String locator) {
String key = "";
WebElement element = null;
try {
key = Utility.fetchLocatorKey(locator);
} catch (Exception e) {
System.out.println("Exception in getText method, " + e.getMessage());
}
if (key.endsWith("id")) {
WebDriverWait wait = new WebDriverWait(driver, 60);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(locator)));
} else if (key.endsWith("cssselector")) {
WebDriverWait wait = new WebDriverWait(driver, 60);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(locator)));
} else if (key.endsWith("linktext")) {
WebDriverWait wait = new WebDriverWait(driver, 60);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText(locator)));
} else if (key.endsWith("xpath")) {
WebDriverWait wait = new WebDriverWait(driver, 60);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(locator)));
}
}