Java Selenium WebDriver By.xpath不';I don’我不能一直工作
信息:Java Selenium WebDriver By.xpath不';I don’我不能一直工作,java,xpath,selenium,webdriver,Java,Xpath,Selenium,Webdriver,信息: <li><input type="password" name="signin[password]" id="signin_password" /></li> 我从配置文件中获得fieldXpath,它是“//输入[@id='signin\u password']” HTML: <li><input type="password" name="signin[password]" id="signin_password" />&
<li><input type="password" name="signin[password]" id="signin_password" /></li>
我从配置文件中获得fieldXpath
,它是“//输入[@id='signin\u password']”
HTML:
<li><input type="password" name="signin[password]" id="signin_password" /></li>
是否有一个解决方案可以使此代码与XPath一起工作 使用单引号而不是
“
”
String fieldXpath = "//input[@id='signin_password']";
我发现了问题…:selenium WebDriver StaleElementReferenceException
*This may be caused because the page isn't loaded completely when the code starts or changes when the code is executed. You can either try to wait a little longer for the element or catch the StaleReferenceException and try again finding the div and the span.*
我的代码:(在每个字段之前调用这些函数)
/**
*句柄StaleElementReferenceException
*@param elementXpath
*@param timetowaitinse
*/
public void staleElementHandleByXpath(字符串elementXpath,int-timeToWaitinse){
整数计数=0;
同时(计数<10){
试一试{
WebElement slipperyElement=driver.findElement(By.xpath(elementXpath));
if(slipperyElement.isDisplayed()){
slipperyElement.click();//可能引发StaleElementReferenceException
}
计数=计数+10;
}捕获(StaleElementReferenceException e){
count=count+1;//再试一次
}捕获(ElementNotVisibleException e){
count=count+10;//出去
}捕获(例外e){
count=count+10;//出去
}最后{
//在执行操作之前,请等待X秒
driver.manage().timeouts().implicitlyWait(timetowaitinse,TimeUnit.SECONDS);
}
}
}
/**
*等到文件真正准备好
*@param js
*@param timetowaitinse
*/
public void waiTillDocumentReadyStateComplete(JavascriptExecutor js,int-TimeToWaitinse){
布尔就绪=假;
整数计数=0;
而(!准备就绪(&count)<10){
ready=(布尔值)js.executeScript(“return document.readyState=='complete';”;
//在执行操作之前,请等待X秒
driver.manage().timeouts().implicitlyWait(timetowaitinse,TimeUnit.SECONDS);
计数=计数+1;
}
}
您可以共享该元素的HTML代码吗?您还可以指定正在使用的浏览器。旧版本的IE与XPath不一致。没有区别,但您也可以尝试fieldXpath=//input[contains(@id,“signin_password”)]谢谢大家,但这是另一个问题:selenium WebDriver StaleElementReferenceException您对这里的答案不满意吗?请接受答案。
/**
* Handle StaleElementReferenceException
* @param elementXpath
* @param timeToWaitInSec
*/
public void staleElementHandleByXpath(String elementXpath, int timeToWaitInSec) {
int count = 0;
while (count < 10) {
try {
WebElement slipperyElement = driver.findElement(By.xpath(elementXpath));
if (slipperyElement.isDisplayed()) {
slipperyElement.click(); // may throw StaleElementReferenceException
}
count = count + 10;
} catch (StaleElementReferenceException e) {
count = count + 1; // try again
} catch (ElementNotVisibleException e) {
count = count + 10; // get out
} catch (Exception e) {
count = count + 10; // get out
} finally {
// wait X sec before doing the action
driver.manage().timeouts().implicitlyWait(timeToWaitInSec, TimeUnit.SECONDS);
}
}
}
/**
* Wait till the document is really ready
* @param js
* @param timeToWaitInSec
*/
public void waiTillDocumentReadyStateComplete(JavascriptExecutor js, int timeToWaitInSec) {
Boolean ready = false;
int count = 0;
while (!ready && count < 10) {
ready = (Boolean) js.executeScript("return document.readyState == 'complete';");
// wait X sec before doing the action
driver.manage().timeouts().implicitlyWait(timeToWaitInSec, TimeUnit.SECONDS);
count = count + 1;
}
}