【问题标题】:Element not visible error with XPathXPath 的元素不可见错误
【发布时间】:2018-10-23 02:10:52
【问题描述】:

我遇到了 XPath 的问题。

我可以使用这个 xpath://*[@id='name']//*[@class='class']//div[1] -> 可以正常运行。

但我想使用 div[2] ex: //*[@id='name']//*[@class='class']//div[2] ,它会给出 元素不可见的错误。 谁能帮帮我,我不知道为什么 div[1] 可以运行但 div[2] 不可见

我的 HTML 代码在这里:

<div class="class">
      <div class="action-item" data-id="24" data-actioncode="STT">
             <a href="#"><i class="fa fa-play"></i></a>
                                S T T
      </div>
      <div class="action-item" data-id="29" data-actioncode="FULL">
              <a href="#"><i class="fa fa-play"></i></a>
                                FULL
      </div>
      <div class="action-item" data-id="30" data-actioncode="TEACHER">
              <a href="#"><i class="fa fa-play"></i></a>
                               TEACHER
      </div>
</div>  

我试过的代码:

WebElement btnElement = driver.findElement(By.xpath("//[@id='name']//[@class='class']//div[2]"));  
WebDriverWait wait= new WebDriverWait(driver,10 ); 
wait.until(ExpectedConditions.visibilityOf(btnElement)); 
btnElement.click();

【问题讨论】:

  • 可以获得 STT xpath 但 FULL xpath 无法运行。我使用的代码:WebElement btnElement=driver.findElement(By.xpath("//[@id="name"]//[@class='class']//div[2]")); WebDriverWait 等待=新的 WebDriverWait(驱动程序,10); wait.until(ExpectedConditions.visibilityOf(btnElement)); btnElement.click();
  • 我猜 xpath //[@id='name']//[@class='class']//div[2] 可以匹配页面上的多个元素,但匹配列表中的第一个不可见,它不是您想要的。您可以手动尝试 xpath 来确认我的猜测。
  • xpath 有问题,您的 xpath 匹配一个或多个元素,请仔细检查重复的 xpath。

标签: java selenium selenium-webdriver xpath webdriver


【解决方案1】:

您也可以使用 CSS 选择器路径,就像 div 2 一样:

div.action-item:nth-child(2)

谢谢。

【讨论】:

    【解决方案2】:

    你可以试试这个代码

    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@data-actioncode='FULL']")));
    driver.findElement(By.xpath("//div[@data-actioncode='FULL']")).click();
    

    【讨论】:

      【解决方案3】:

      根据您共享的 HTML,要单击文本为 FULL 的元素,您必须诱导 WebDriverWait 元素才能被点击,并且您可以使用以下定位器:

      • css_selector(基于属性):

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.class div.action-item[data-actioncode='FULL']"))).click();
        
      • xpath(基于属性):

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='class']//div[@class='action-item' and @data-actioncode='FULL']"))).click();
        
      • xpath(基于文本):

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='class']//div[@class='action-item'][normalize-space()='FULL']"))).click();
        

      【讨论】:

        【解决方案4】:

        你可以利用action-item类来验证。

        使用 x-path 来识别类名,然后做一个第 n 个孩子:

        xpath("//[@id='name']//[@class='class']//[@class='action-item']:nth-child(2)"));
        

        或者你也可以使用索引值,因为它们在同一个类中。 不同的是,第二项的索引值为 1。

        或者,您也可以使用 cssContainText 来通过文本进行识别。

        【讨论】:

          猜你喜欢
          • 2016-10-21
          • 2015-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多