Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium WebDriver在Amazon站点上运行不正常_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java Selenium WebDriver在Amazon站点上运行不正常

Java Selenium WebDriver在Amazon站点上运行不正常,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我正试图针对Amazon站点编写一个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")

我正试图针对Amazon站点编写一个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”。有什么问题?我如何解决它呢?

您是否尝试过通过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");

    }

您试图用
id
作为
nav link yourcount
单击的元素不可单击。要继续,您需要单击带有文本
Hola的链接。Identifícate
或使用以下
xpath
s之一的文本链接
Mi cuenta

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


在单击此webelement之前,请等待几秒钟,以便您的驱动程序能够找到该webelement

对于等待,我使用
显式等待
方法

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();

应用等待直到元素出现,这样它就避免了NoTouchElementException,代码工作时不会出现任何错误

以下代码工作正常:

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");
}

尝试使用显式等待登录元素,而不是使用隐式等待

我尝试过显式等待超过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");
            }
        }

    }
}
公共类转储{
公共静态void main(字符串a[]{
System.setProperty(“webdriver.gecko.driver”,“C:\\geckodriver-v0.18.0-win64\\geckodriver.exe”);
WebDriver=newfirefoxdriver();
WebDriverWait wait=新的WebDriverWait(驱动程序,15);

对于(int i=0;我添加了一些隐式等待,并再次尝试使用一些隐式等待
driver.manage()(15,TimeUnit.SECONDS);如您所示,但仍不起作用。@mvillegas您收到任何错误吗?我已尝试使用driver.findElement(By.xpath(“./*[@id='nav-link-accountList'])。单击();并且它并不总是工作正常。有时OK,有时KO无法定位元素:./*[@id='nav-link-accountList']如果有id,那么为什么要使用xpath?有什么具体原因吗?我粘贴了您的代码,并收到以下错误:线程“main”中出现异常org.openqa.selenium.NoSuchElementException:找不到元素:#nav \-link \-accountList有关此错误的文档,请访问:在哪个浏览器上出现错误,在2.53 Jarsty上工作正常此代码正常,我在加载网站后进行了一些等待。
try
在错误行附近。它应该在等待行附近。另外,2.53真的很老了…你应该更新到最新版本。这不是必要的,杰夫,它工作得很好,反正没有理由否决,没有问题。人们可以在最新的罐子上使用它。阿尔索伊写道:试试看{driver.findElement(By.linkText(//a[@id=\“nav link yourcount\”)/span[contains(text(),'Mi cuenta')])))。单击();}捕获(异常e){e.printStackTrace();System.out.println(“未找到”);}和KO再次出现此错误信息ciÓN:检测到的方言:W3C org.openqa.selenium.NoSuchElementException:无法定位元素://a[@id=“nav link yourcount”]/span[contains(text(),'Mi cuenta')]有关此错误的文档,请访问:Build info:version:'unknown',revision:'unknown',time:'unknown'我粘贴了您的代码,但效果不好。此错误:INFORMACIÓN:检测到的方言:W3C org.openqa.selenium.TimeoutException:预期条件失败:等待元素可单击:By.xpath://*[@id='nav-link-accountList'](每隔500毫秒尝试15秒)
wait.until()
返回等待的元素,以便您可以执行
wait.until(…)。单击()
并保存一些代码,同时不必刮页面两次。如果此答案对您有效,请将其标记为
已接受
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");
            }
        }

    }
}