gpt4 book ai didi

java - 需要帮助使用 webdriver 和 java 识别灯箱上的控件

转载 作者:太空宇宙 更新时间:2023-11-04 13:22:25 26 4
gpt4 key购买 nike

我正在尝试自动化网站的登录流程。当我单击“登录”时,会弹出一个灯箱,并且我的脚本无法识别弹出窗口中的元素。该网站是 www.zillow.com,当我单击右上角的“登录”链接时,就会发生这种情况。

我尝试使用 JavascriptExecutor、等待和其他一些方法,但无法识别。这里需要帮助。谢谢!我得到的错误如下。

public class Trial {

public static void main(String [] args){

WebDriver driver = new FirefoxDriver();
driver.get("http://www.zillow.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[@id='login_opener']/span")).click();
driver.findElement(By.xpath("//*[@id='email']")).sendKeys("abcde");
driver.findElement(By.xpath("//*[@id='password']']")).sendKeys("abcde");
}
}

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='email']"}
Command duration or timeout: 20.51 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16'
System info: host: 'pc-linux-ravip', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-65-generic', java.version: '1.7.0_79'
*** Element info: {Using=xpath, value=//*[@id='email']}
Session ID: 3b861869-4a8b-4cd1-b051-bab213d02077
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=41.0.1}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:445)
at org.openqa.selenium.By$ByXPath.findElement(By.java:358)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
at com.trials.testng.learning.Trial.main(Trial.java:18)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='email']"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16'
System info: host: 'pc-linux-ravip', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-65-generic', java.version: '1.7.0_79'
Driver info: driver.version: unknown
at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///tmp/anonymous4582481678202576888webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10667)
at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///tmp/anonymous4582481678202576888webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:623)

最佳答案

我发现您的 Xpath 格式错误。我稍微修改了你的代码,效果很好

 WebDriver driver = new FirefoxDriver();
driver.get("http://www.zillow.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//span[contains(text(),'Sign in')]")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[@id='email']")).sendKeys("abcde");
driver.findElement(By.xpath("//input[@id='password']")).sendKeys("abcde");

我更改了以下 xpath,并在单击“登录”按钮后引入了一个隐式等待

  1. //span[contains(text(),'Sign in')] 标识登录按钮。
  2. //输入[@id='email']来标识电子邮件文本框
  3. //输入[@id='password']识别密码文本框

关于java - 需要帮助使用 webdriver 和 java 识别灯箱上的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32959183/

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