如何使用Selenium和Java定位元素



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

元素的 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:UsersHpAppDataLocalTempscoped_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]}

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

#nav-link-accountList

您可能正在尝试将文本作为"登录 amazon.in"的元素click(),并为此需要

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

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

您可以使用下面的 xpath 来选择链接

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

或者你应该擅长

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

CSS定位器

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

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

给定元素有一个 id。因此,您可以通过id直接定位元素。

使用 ID:

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

使用 css 选择器:

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

使用 XPath:

Webdriver driver = new ChromeDriver();
driver.findElement(By.xpath("//*[@id='nav-link-accountList']"));

最新更新