Javascript 在Selenium中通过jQuery中的generate获取元素

Javascript 在Selenium中通过jQuery中的generate获取元素,javascript,java,jquery,selenium,xpath,Javascript,Java,Jquery,Selenium,Xpath,我有一个文本框。它只接受数字类型。键入字符串并sendKeys(Keys.TAB)时,将显示一条错误消息弹出窗口。此弹出窗口由jQuery附加,带有id=“message\u eff”,以前不存在 我尝试使用以下代码: driver.findElement(By.xpath(".//*[@id='messagefor_eff']")); 然后运行我的测试。 我运行了10次测试,但有时会出错: org.openqa.selenium.NoSuchElementException: Unable

我有一个文本框。它只接受数字类型。键入字符串并
sendKeys(Keys.TAB)
时,将显示一条错误消息弹出窗口。此弹出窗口由jQuery附加,带有
id=“message\u eff”
,以前不存在

我尝试使用以下代码:

driver.findElement(By.xpath(".//*[@id='messagefor_eff']")); 
然后运行我的测试。 我运行了10次测试,但有时会出错:

org.openqa.selenium.NoSuchElementException: Unable to locate element {"method":"xpath","selector":".//*[@id='messagefor_eff']"}
如何修复此错误并使此错误再次出现?

在定位动态生成的元素之前,请尝试合并:

WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='messagefor_eff']")));
driver.findElement(By.xpath(".//*[@id='messagefor_eff']")); 
在定位动态生成的元素之前,请尝试合并:

WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='messagefor_eff']")));
driver.findElement(By.xpath(".//*[@id='messagefor_eff']")); 

在查找元素之前,您可能需要切换到警报:

wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
driver.findElement(By.xpath(".//*[@id='messagefor_eff']")); 

请参阅:

在查找元素之前,您可能需要切换到警报:

wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
driver.findElement(By.xpath(".//*[@id='messagefor_eff']")); 

请参阅:

在尝试查找错误消息之前,是否已给予足够的超时?是。我添加了
Thread.sleep(2000)在它之前。我猜当Selenium启动浏览器时,Javascript没有启用。您是否可以尝试将浏览器所需功能中的javascript enabled设置为true,然后重试该测试?在尝试查找错误消息之前,您是否已给予足够的超时时间?是。我添加了
Thread.sleep(2000)在它之前。我猜当Selenium启动浏览器时,Javascript没有启用。您是否可以尝试将浏览器所需功能中的javascript enabled设置为true,然后重试测试?