gpt4 book ai didi

java - 为什么我的测试抛出 Exception-Unable to locate element in webdriver?

转载 作者:行者123 更新时间:2023-11-29 05:31:21 25 4
gpt4 key购买 nike

package testproject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.*;
public class mytestclass {
public static void main(String[] args) {
WebDriver Driver = new FirefoxDriver();
Driver.get("https://www.gmail.com/");
WebElement wb= Driver.findElement(By.name("Email"));
wb.sendKeys("sweta");
WebElement wb1= Driver.findElement(By.name("Passwd"));
wb1.sendKeys("123456");
WebElement wb2= Driver.findElement(By.id("signIn"));
wb2.click();
WebElement wb3= Driver.findElement(By.xpath(".//*[@id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a"));
wb3.click();
WebElement wb4= Driver.findElement(By.id("gb_71"));
wb4.click();
}
}

当我执行此代码时,一切正常,直到我希望单击登录按钮为止。我收到异常说明线程“main”中的异常 org.openqa.selenium.NoSuchElementException:无法定位元素:{“method”:“xpath”,“selector”:".//*[@id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a"} 但是当我尝试使用 fierbug 定位它时它工作正常。在上面提到的代码中,我更改了电子邮件 ID 和密码以确保电子邮件安全。

我已经在 stakwave 上发布了另一个程序时遇到问题,所以如果可以的话,请查看此链接 - webdriver is not able to click on a hyperlink in firefox

最佳答案

您确定您的页面在您登录后已完全加载吗?

您是否为您的网络驱动程序设置了超时? (它必须等待元素多长时间)。可能它会在完全加载之前读取您的 html。

Webdriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

要快速找出问题所在,请在执行 wb2.click();

后执行 Thread.sleep(8000)

关于java - 为什么我的测试抛出 Exception-Unable to locate element in webdriver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20998375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com