如何使用Selenium和Java定位元素

问题描述 投票:0回答:4

我没有在给定代码中识别定位器来执行自动化测试。

元素的HTML:

<a href="https://www.amazon.in/ap/signin?openid.pape.max_auth_age=0&openid.retu…%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&" class="nav-a nav-a-2" data-nav-ref="nav_ya_signin" data-nav-role="signin" data-ux-jq-mouseenter="true" id="nav-link-accountList" tabindex="25">

错误堆栈跟踪:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='nav-link-accountList']/span[3]"}
  (Session info: chrome=73.0.3683.86)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.17134 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.10 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'LAPTOP-118FCQKH', ip: '192.168.43.44', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9), userDataDir=C:\Users\Hp\AppData\Local\Temp\scoped_dir5888_21816}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=73.0.3683.86, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 107d4a42af64419e98016e7927c7dde3
*** Element info: {Using=xpath, value=//*[@id='nav-link-accountList']/span[3]}
java selenium xpath css-selectors webdriverwait
4个回答
1
投票

有一个id,所以使用它作为最快的选择器方法

#nav-link-accountList

0
投票

可能你正试图在click()登录文本元素的amazon.in并实现你需要:

  • 鼠标将元素悬停在文本作为您的订单/帐户和列表
  • 单击带有文本的元素作为登录
  • 您可以使用以下解决方案: qazxsw poi

0
投票

您可以使用以下xpath选择链接

System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.amazon.in");
new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='nav-tools']/a//span[@class='nav-line-2']")))).build().perform();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='nav-action-inner' and text()='Sign in']"))).click();

或者你应该很好

//a[@class='nav-a nav-a-2' and @data-nav-role='signin']

CSS定位器

//a[@data-nav-role='signin']

0
投票

看起来您正在尝试单击您给出的锚元素下的第三个span元素。给出错误的XPath不适用于您给出的元素。

给定元素具有id。所以你可以通过id直接找到元素。

使用ID:

a[data-nav-role='signin']

使用Css选择器:

Webdriver driver = new ChromeDriver();    
driver.findElement(By.id("nav-link-accountList"));

使用XPath:

Webdriver driver = new ChromeDriver();
driver.findElement(By.cssSelector("#nav-link-accountList"));
© www.soinside.com 2019 - 2024. All rights reserved.