0

我收到此错误:

org.openqa.selenium.StaleElementReferenceException:过时的元素引用:元素未附加到页面文档。

我尝试使用Thread网站上给出的 a 和其他解决方案。仍然收到错误。

在以下两种情况之一中会引发过时的元素引用异常,第一种比第二种更常见:

该元素已被完全删除。
该元素不再附加到 DOM。

我的代码是:

try {
    driver.navigate().to("http://uk.support.tomtom.com/");

    driver.findElement(By.xpath("//a[contains(text(),'Continue In UK')]")).click();
    System.out.println("Clicked on Continue in UK");

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    driver.findElement(By.xpath("//button/span[contains(text(),'Support')]")).click();
    System.out.println("clicked on support");

    WebDriverWait wait = new WebDriverWait(driver, 10);

    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//h5[contains(text(),'Contact')]/parent::div//a/div[contains(text(),'Contact')]")));
    driver.findElement(By.xpath("//h5[contains(text(),'Contact')]/parent::div//a/div[contains(text(),'Contact')]")).click();
    System.out.println("Clicked on Contact");

    driver.findElement(By.xpath("//h4[contains(text(),'Navigation devices')]")).click();
    System.out.println("Clicked on Navigation devices");

    driver.findElement(By.xpath("//h4[text()='Car']")).click();
    System.out.println("Clicked on Car");

    driver.findElement(By.xpath("//h4[text()='TomTom GO']")).click();
    System.out.println("Clicked on Go");

    driver.findElement(By.xpath("//span[text()='TomTom GO 6200 Wi-Fi']")).click();
    System.out.println("Click on device");

    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//select[@id='contact_reason_list']"))));
    Select dropdown = new Select(driver.findElement(By.xpath("//select[@id='contact_reason_list']")));
    List<WebElement> options = dropdown.getOptions();
    for(int i=1; i<options.size();i++)
    {
        if(options.get(i).getText().contains("Warranty and repairs"))
        {
            dropdown.selectByIndex(i);
            break;
        }
    }
    System.out.println("Clicked on Warranty and repair");

    wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//li[@id='email_us_option']//button//span[contains(text(),'Email us')]/preceding-sibling::span/span/img"))));
    driver.findElement(By.xpath("//li[@id='email_us_option']//button//span[contains(text(),'Email us')]/preceding-sibling::span/span/img")).click();
    System.out.println("Clicked on Email us");

    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.name("IDToken1"))));
    driver.findElement(By.name("IDToken1")).click();
    driver.findElement(By.name("IDToken1")).clear();
    driver.findElement(By.name("IDToken1")).sendKeys("niharika.gaur@tomtom.com");
    System.out.println("print email");

    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.name("IDToken2"))));
    driver.findElement(By.name("IDToken2")).click();
    driver.findElement(By.name("IDToken2")).clear();
    driver.findElement(By.name("IDToken2")).sendKeys("tomtom");
    System.out.println("print password");

    driver.findElement(By.xpath("//div[@class='tt-form-item is-button']/button[contains(text(),Login)]")).click();
    System.out.println("Clicked on Login");

    Thread.sleep(5000);

    System.out.println("Pass");
    wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//input[@name='Incident.Subject']"))));
    driver.findElement(By.xpath("//input[@name='Incident.Subject']")).click();
    driver.findElement(By.xpath("//input[@name='Incident.Subject']")).clear();
    driver.findElement(By.xpath("//input[@name='Incident.Subject']")).sendKeys("Test");
    System.out.println("done1");

} catch(WebDriverException wde){
    wde.printStackTrace();

} catch (Exception e) {
    e.printStackTrace();
    // TODO: handle exception
}
4

0 回答 0