简体   繁体   中英

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. 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

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. Try adding wait before 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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