Java Selenim chrome驱动程序无法定位嵌套<;李>;标签

Java Selenim chrome驱动程序无法定位嵌套<;李>;标签,java,selenium-webdriver,xpath,css-selectors,webdriverwait,Java,Selenium Webdriver,Xpath,Css Selectors,Webdriverwait,我试图找到嵌套的nl标记,但我无法从by.xpath查询中获取它 下面是我用java编写的相关代码行 driver.findElement(By.xpath("//div[@id='navbarNav']/following-sibling::ul[1]/li[2]")).click(); ` 我想匹配: <a class="sub-nav-link style-scope app-shell active" href="#/trend-

我试图找到嵌套的
nl
标记,但我无法从
by.xpath
查询中获取它

下面是我用java编写的相关代码行

driver.findElement(By.xpath("//div[@id='navbarNav']/following-sibling::ul[1]/li[2]")).click();
` 我想匹配:

<a class="sub-nav-link style-scope app-shell active" href="#/trend-analysis/tag-search">Trend Analysis
相关HTML:

<div class="collapse navbar-collapse style-scope app-shell" id="navbarNav">
                <ul class="app-nav navbar-nav mr-auto style-scope app-shell">
                    <li class="nav-item style-scope app-shell">
                        <a class="nav-link p-3 px-4 style-scope app-shell" href="/#/dashboard">Dashboard</a>
                    </li>
                    <li class="nav-item style-scope app-shell">
                        <a class="nav-link py-3 px-4 style-scope app-shell active" href="/#/trend-analysis/tag-search">TrendAnalysis</a>
                        <ul class="sub-nav d-flex flex-row flex-nowrap list-unstyled style-scope app-shell">
                            <li class="sub-nav-item style-scope app-shell">
                                <a class="sub-nav-link style-scope app-shell active" href="#/trend-analysis/tag-search">
                                    Trend Analysis
                                </a>
                            </li>
                            <li class="sub-nav-item style-scope app-shell">
                                <a class="sub-nav-link style-scope app-shell" href="#/trend-analysis/value-based-search">
                                    Value-Based Search
                                </a>
                            </li>
                  .....
更新:

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: //div[@id='navbarNav']/ul (tried for 30 second(s) with 500 milliseconds interval)
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:13:22.693Z'
System info: host: 'root', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.15.0-118-generic', java.version: '1.8.0_265'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 85.0.4183.102, chrome: {chromedriverVersion: 85.0.4183.87 (cd6713ebf92fa..., userDataDir: /tmp/.com.google.Chrome.7m6LU4}, goog:chromeOptions: {debuggerAddress: localhost:38779}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: 6e2d32881f709e2e0d727d8cc45813c4
        at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:113)
        at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:283)
        at GettingStarted.main(GettingStarted.java:103)
更新2:

当我使用以下命令运行代码时:

driver.findElement(By.xpath("//div[@id='navbarNav']//li/a[contains(@href,'tag-search')]")).click();
我得到:

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable
  (Session info: headless chrome=85.0.4183.102)

在单击元素之前添加一些等待

String myElement = "(//div[@id='navbarNav']//li/a[contains(@href,'tag-search')])[1]";    
WebDriverWait wait = new WebDriverWait(driver, 30);
 wait.until(ExpectedConditions.elementToBeClickable(By.xpath(myElement)));

您还需要使用xpath以正确的锚元素为目标

driver.findElement(By.xpath(myElement)).click()
使用Threads.sleep(10)检查时间元素是否可单击,如果元素不可单击,则应该是您创建的xpath错误

但如果它正在单击,则使用2个并排等待 像这样:

String myElement = "(//div[@id='navbarNav']//li/a[contains(@href,'tag-search')])[1]";    
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(myElement)));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(myElement)));
driver.findElement(By.xpath(myElement)).click()
在文本为趋势分析的元素上单击()

  • linkText

    driver.findElement(By.linkText("Trend Analysis")).click();
    
    driver.findElement(By.partialLinkText("Trend Analysis")).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Trend Analysis"))).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Trend Analysis"))).click();
    
  • partialLinkText

    driver.findElement(By.linkText("Trend Analysis")).click();
    
    driver.findElement(By.partialLinkText("Trend Analysis")).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Trend Analysis"))).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Trend Analysis"))).click();
    
  • css选择器

    driver.findElement(By.cssSelector("div#navbarNav li.sub-nav-item.style-scope.app-shell>a[href$='tag-search']")).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#navbarNav li.sub-nav-item.style-scope.app-shell>a[href$='tag-search']"))).click();
    
  • xpath

    driver.findElement(By.xpath("//div[@id='navbarNav']//li[@class='sub-nav-item style-scope app-shell']/a[contains(@href, 'tag-search')][contains(., 'Trend Analysis')]")).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='navbarNav']//li[@class='sub-nav-item style-scope app-shell']/a[contains(@href, 'tag-search')][contains(., 'Trend Analysis')]"))).click();
    
理想情况下,要
单击
元素所需的元素,以使
元素可伸缩()
,您可以使用以下任一选项:

  • linkText

    driver.findElement(By.linkText("Trend Analysis")).click();
    
    driver.findElement(By.partialLinkText("Trend Analysis")).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Trend Analysis"))).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Trend Analysis"))).click();
    
  • partialLinkText

    driver.findElement(By.linkText("Trend Analysis")).click();
    
    driver.findElement(By.partialLinkText("Trend Analysis")).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Trend Analysis"))).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Trend Analysis"))).click();
    
  • css选择器

    driver.findElement(By.cssSelector("div#navbarNav li.sub-nav-item.style-scope.app-shell>a[href$='tag-search']")).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div#navbarNav li.sub-nav-item.style-scope.app-shell>a[href$='tag-search']"))).click();
    
  • xpath

    driver.findElement(By.xpath("//div[@id='navbarNav']//li[@class='sub-nav-item style-scope app-shell']/a[contains(@href, 'tag-search')][contains(., 'Trend Analysis')]")).click();
    
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='navbarNav']//li[@class='sub-nav-item style-scope app-shell']/a[contains(@href, 'tag-search')][contains(., 'Trend Analysis')]"))).click();
    

我已使用您建议中的新错误更新了我的问题。谢谢,但我在等待元素可单击时遇到了相同的错误
尝试使用不同的预期条件,如线程“main”org.openqa.selenium.elementNotInteractivatableException:元素不可交互,另一个问题是
标签搜索
不是唯一的,因为
标签搜索第一次出现在
/html/body/app shell/nav/div/ul[1]/li[2]/a
和第二次出现在
/html/body/app shell/nav/div/ul[1]/li[2]/ul[1]/a
我相信这就是它不匹配的原因。我需要限制它首先匹配。看起来,我已经尝试了您上面共享的大多数示例,我的代码也失败了,
org.openqa.selenium.TimeoutException:Expected condition failed:waiting element to clickable:By.linkText:Trend Analysis(尝试了20秒,间隔500毫秒)
对于
ExpectedConditions
org.openqa.selenium.elementNotInteractivatableException:element not interactivatable
当使用
findElement
函数时。线程“main”org.openqa.selenium.TimeoutException中的异常:预期条件失败:等待元素可点击:By.xpath:(//div[@id='navbarNav']//li/a[contains(@href,'tag-search')][1](以500毫秒的间隔尝试了30秒)您是否尝试了线程。睡眠检查您传递的元素是否正在单击?@USE