【问题标题】:XPath failing to select elementXPath 无法选择元素
【发布时间】:2018-08-05 10:03:17
【问题描述】:

谁能给出这个 div 元素的更好的 xpath,或者帮助解释为什么我似乎无法选择所需的 div?

此 XPath 不起作用:

//div[starts-with(normalize-space(.),'Welcome to the Shipt Shopper') and @class='text']

即使它在 chrome 开发工具中突出显示,也会抛出 NoSuchElementException。

页面上的所有元素都是这种情况

来自页面的 HTML 的 sn-p,其中包含我要定位的内容:

<div class="content-wrapper">
    <div class="content" style="padding-top: 116px;">                               
        <div class="media">         
          <div class="attachment" data-attachment="{&quot;image&quot;:&quot;https:\/\/images.typeform.com\/images\/29rsVwT3VF\/image\/default#.png&quot;,&quot;width&quot;:360,&quot;height&quot;:137,&quot;video_source&quot;:&quot;&quot;,&quot;video_id&quot;:&quot;&quot;}" style="width: 360px; height: 137px;">
<img src="https://images.typeform.com/images/29rsVwT3VF/image/default#.png" data-original="https://images.typeform.com/images/29rsVwT3VF/image/default#.png" style="width: 360px; height: 137px; display: inline;">
  </div>
</div>                      
        <div class="text" style="padding-top: 30px; margin-left: 0px;">
                                                    Welcome to the Shipt Shopper application! <br><br>Ready to get started?

        </div>
        <div class="button-wrapper" style="margin-top: 30px;">
          <div class="button general full enabled hover-effect" style="">Begin</div>
        <div class="button-text">press <strong>ENTER</strong></div>
      </div>                    
    </div>
</div>

【问题讨论】:

  • 元素是动态注入的吗?
  • 图片中的html
  • ...是的,我问的是 HTML 的那部分是否通过一些外部事件(例如按钮单击或弹出)注入到 DOM 中。
  • 如果您发布 HTML 的 sn-p 会有所帮助,而不仅仅是开发人员工具的屏幕截图。例如,我们可以更容易地确定引号是否是文本的一部分。
  • @SharonJones 您需要等待 iframe 完成加载。使用将自动切换到 iframe 的 frameToBeAvailableAndSwitchToIt(String frameLocator) 预期条件。然后使用查询查找欢迎文本。 seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/…

标签: selenium xpath selenium-webdriver


【解决方案1】:

根据HTML,您共享的xpath 是有效且正确的。但是当您尝试通过 Selenium 与之交互时,将抛出 NoSuchElementException

要通过 Selenium 与元素交互,您可以使用以下xpath

//div[@class='content-wrapper']/div[@class='content']//div[@class='text' and contains(normalize-space(), 'Welcome to the Shipt Shopper application')]

但是查看 HTML 似乎您必须诱导 WebDriverWait 才能使元素可见,如下所示:

WebElement welcomeShiptShopper = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='content-wrapper']/div[@class='content']//div[@class='text' and contains(normalize-space(), 'Welcome to the Shipt Shopper application')]")));

网页在我结束时没有打开。正如其他人所指出的那样,WebElement 可能在 &lt;iframe&gt; 标记内,在这种情况下,您必须首先使用以下任一方法切换到适当的 iframe

  • 切换Frame Name

    driver.switchTo().frame("frame_name");
    
  • 切换Frame ID

    driver.switchTo().frame("frame_id");
    
  • 切换Frame Index

    driver.switchTo().frame(1);
    

切换到合适的框架后,现在您可以使用上述建议的xpaths 来寻找WebElement

【讨论】:

    【解决方案2】:

    您所定位的内容位于&lt;iframe&gt; 中。

    我不熟悉如何配置 Selenium,但看起来您可能需要切换到该框架。由于它没有@id,您可能需要按位置选择:

    driver.switchTo().frame(0) 
    

    然后执行 XPath。

    完成后,跳回包含的 HTML 页面:

    driver.switchTo().defaultContent();
    

    【讨论】:

    • 没有帮助...同样的 NoSuchElementException
    • 您的 XPath 看起来不错,我认为问题出在 iframe 上。要么需要切换上下文和/或正如@Grasshopper 建议的那样,等到它加载完毕。
    • 没有意识到它的框架。让我尝试切换到框架
    猜你喜欢
    • 2020-08-06
    • 1970-01-01
    • 2020-03-05
    • 2020-03-01
    • 1970-01-01
    • 2020-08-08
    • 2012-03-18
    • 1970-01-01
    • 2022-07-01
    相关资源
    最近更新 更多