简体   繁体   English

动作类在Selenium Webdriver中以调试模式工作

[英]Action class works in debug mode in selenium webdriver

I have written the code and that works well when I run this is debugging mode but when I run it in normal mode then I am getting the following exception 我已经编写了代码,在调试模式下运行时效果很好,但是在正常模式下运行时,出现以下异常

     org.openqa.selenium.NoSuchElementException: no such element: 
     Unable to locate element: {"method":"xpath","selector":".//*[@id='address-0']/span"}

The code I have written is: 我写的代码是:

    WebElement searchBox  = driver.findElement(By.id("search-input"));
    searchBox.sendKeys("somepostcode");
    Actions actions = new Actions(driver);
    actions.moveToElement(searchBox);
    WebElement address = driver.findElement(By.xpath(".//*[@id='address-0']/span"));
    actions.moveToElement(address);
    actions.click();
    actions.perform();

I am not able to understand where should I put wait. 我无法理解应该在哪里等待。

I am using eclipse IDE. 我正在使用Eclipse IDE。 The functionality works like when I put some postcode in the search box it search for some addresses at runtime and the user has to select any address related to the postcode. 该功能的工作原理类似于将一些邮政编码放入搜索框中时,它会在运行时搜索一些地址,并且用户必须选择与该邮政编码相关的任何地址。 Ajax has been used to fetch the postcode Ajax已被用于获取邮政编码

Here search box is a textbox. 这里的搜索框是一个文本框。

Please let me know if more information is required. 如果需要更多信息,请告诉我。

尝试在WebElement address = driver.findElement(By.xpath(".//*[@id='address-0']/span"));之前添加一些等待时间WebElement address = driver.findElement(By.xpath(".//*[@id='address-0']/span"));

Error tells you that, you are trying to create an instance of WebElement "address" before its visible on the page. 错误告诉您,您正在尝试创建WebElement“ address”实例,然后在页面上将其可见。 Try adding wait before WebElement address = driver.findElement(By.xpath(".//*[@id='address-0']/span")); 尝试在WebElement address = driver.findElement(By.xpath(".//*[@id='address-0']/span"));之前添加等待WebElement address = driver.findElement(By.xpath(".//*[@id='address-0']/span"));

In cases like this, when the script works in debug mode but fails during normal it is almost always the issue with timing. 在这种情况下,当脚本在调试模式下运行但在正常模式下失败时,几乎总是时序问题。 So your page is just not fully loaded at the time you are trying to locate that element. 因此,在尝试查找该元素时,页面只是没有完全加载。

Place an explicit wait just before your problematic element. 在有问题的元素之前进行明确的等待。 It's usually not the best practice to use explicit wait but you can do it as a quick try to see if that solves your problem. 通常,使用显式等待不是最佳实践,但是您可以快速尝试一下,看看是否可以解决您的问题。 If that does you can refactor it into a sturdier solution later. 如果是这样,您可以稍后将其重构为更坚固的解决方案。

WebElement myDynamicElement = (new WebDriverWait(driver, 10))

Hope this will help you.. 希望这个能对您有所帮助..

    WebElement searchBox  = driver.findElement(By.id("search-input"));

    searchBox.sendKeys("somepostcode");

    Actions actions = new Actions(driver);

    actions.moveToElement(searchBox);

    WebDriverWait wait = new WebDriverWait(driver, 20);

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='address-0']/span")));

    WebElement address = driver.findElement(By.xpath(".//*[@id='address-0']/span"));

    actions.moveToElement(address);

    actions.click();

    actions.perform();

I have solved this problem by breaking the postcode in two parts 我通过将邮政编码分成两部分来解决了这个问题

searchBox.sendKeys("postcodePart1");
searchBox.sendKeys("postcodePart2");

There must be kind of on change event was calling. 一定有一个on change事件正在调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM