【问题标题】:Selenium WebDriver not working fine on Amazon siteSelenium WebDriver 在亚马逊网站上无法正常工作
【发布时间】:2017-08-18 11:28:05
【问题描述】:

我正在尝试针对亚马逊网站编写 Selenium 测试。我想获取“登录”元素,以便我可以点击它。

网址:www.amazon.es

这是我的 Selenium 代码:​​

System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.es");

try
{           
    driver.findElement(By.id("nav-link-accountList")).click();
}
catch (Exception e)
{
    System.out.println("Not Found");
}

有时代码可以正常工作,但有时找不到 ID“nav-link-yourAccount”。问题是什么?我该如何解决?

【问题讨论】:

  • 添加一些隐式等待并重试
  • 尝试使用一些implicitWait driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);让我知道它是否有效
  • 您也可以使用预期条件使测试等待按钮变为可用stackoverflow.com/a/36596333/1501613
  • 我添加了driver.manage()。超时()。 ImplicitlyWait (15, TimeUnit.SECONDS);正如您所说,但仍然无法正常工作。
  • @mvillegas 你有什么错误吗?

标签: java selenium selenium-webdriver


【解决方案1】:

提供几秒钟的wait,然后单击此网络元素,以便您的驱动程序能够找到该网络元素。

等待我使用Explicit wait 方法。

WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("nav-link-accountList"))));

driver.findElement(By.id("nav-link-accountList")).click();

【讨论】:

  • 如果对您有用,请将此答案标记为Accepted
【解决方案2】:

您尝试使用id 作为nav-link-yourAccount 点击的元素不可点击。要继续进行,您需要使用以下xpaths 之一单击带有文本Hola. Identifícate 的链接或带有文本Mi cuenta 的链接:

//a[@id='nav-link-yourAccount']/span[text()='Hola. Identifícate']

//a[@id="nav-link-yourAccount"]/span[contains(text(),'Mi cuenta')]

【讨论】:

  • 我写道:尝试 { driver.findElement(By.linkText("//a[@id=\"nav-link-yourAccount\"]/span[contains(text(),'Mi cuenta')]")).click(); }catch (Exception e) { e.printStackTrace(); System.out.println("未找到"); }
  • 并再次出现此错误 INFORMACIÓN: Detected dialect: W3C org.openqa.selenium.NoSuchElementException: Unable to locate element: //a[@id="nav-link-yourAccount"]/span [contains(text(),'Mi cuenta')] 有关此错误的文档,请访问:seleniumhq.org/exceptions/no_such_element.html Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
【解决方案3】:

改为使用隐式等待尝试使用 explicit 等待登录元素。

我已经尝试过显式等待超过 50 次点击,它确实有效。

这是您可以使用的代码。

public class dump {
    public static void main(String a[]){
        System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        WebDriverWait wait = new WebDriverWait(driver, 15);
        for(int i=0; i<=50; i++){
            driver.get("https://www.amazon.es");
            try{    
                wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='nav-link-accountList']")));  
                driver.findElement(By.xpath("//*[@id='nav-link-accountList']")).click();
                System.out.println("clicked\t"+i);
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("Not Found");
            }
        }

    }
}

这是运行证明:

一切顺利!

【讨论】:

  • 我粘贴了您的代码,但它不能正常工作。这个错误:INFORMACIÓN: Detected dialect: W3C org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //*[@id='nav-link-accountList'] 15 秒,间隔 500 毫秒)
  • wait.until() 返回等待的元素,因此您可以执行 wait.until(...).click() 并节省一些代码,而不必两次抓取页面。
【解决方案4】:

应用等到元素出现,以避免 NoSuchElementException 并且代码正常工作。

以下代码运行良好:

driver.get("https://www.amazon.es");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement accontButton=driver.findElement(By.id("nav-link-accountList"));
WebDriverWait waitforelement=new WebDriverWait(driver,20);
waitforelement.until(ExpectedConditions.elementToBeClickable(accontButton));
try{           
accontButton.click();
}

catch (Exception e){
System.out.println("Not Found");
}

【讨论】:

  • 我粘贴了您的代码并收到此错误:线程“main”中的异常 org.openqa.selenium.NoSuchElementException:无法找到元素:#nav\-link\-accountList 有关此错误的文档,请访问:seleniumhq.org/exceptions/no_such_element.html
  • 在哪个浏览器上出现错误,在 2.53 jars 上运行良好
  • 试试这段代码没问题,我在加载网站后稍等片刻。
  • try 在错误的行附近。它应该在等待线附近。另外,2.53 真的太旧了……你应该更新到最新版本。
  • 没有必要杰夫它工作正常,无论如何都没有理由反对,没有问题。也可以在最新的罐子上使用它
【解决方案5】:

您是否尝试过通过 xpath 查找元素?

System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.es");

try
{           
        driver.findElement(By.xpath("//*[@id='nav-link-accountList']")).click();

}catch (Exception e)
 {
        System.out.println("Not Found");

    }

【讨论】:

  • 我试过 driver.findElement(By.xpath(".//*[@id='nav-link-accountList']")).click();它并不总是运作良好。有时 OK,有时 KO 无法定位元素:.//*[@id='nav-link-accountList']
  • 如果有 id 那么为什么要 xpath 呢?任何具体原因
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-06
  • 2018-10-01
  • 2020-05-01
  • 1970-01-01
相关资源
最近更新 更多