简体   繁体   English

Xpath Select 元件 Selenium

[英]Xpath Select Element Selenium

i am trying get a element through selenium with code:我正在尝试通过 selenium 获取一个元素,代码如下:

WebElement a = driver.findElement(By.xpath("//div[@id=':r6']/span/text()"));

using this same expression on a firefox plugin, the element is find, but in selenium(java code) this way the element is not found, someone can me help在 firefox 插件上使用相同的表达式,可以找到元素,但是在 selenium(java 代码)中找不到元素,有人可以帮帮我

The command that you might need is: "AllowNativeXPath" - then just use the Xpath (either via Xpather or after 'inspecting element')to identify your element.您可能需要的命令是:“AllowNativeXPath” - 然后只需使用 Xpath(通过 Xpather 或在“检查元素”之后)来识别您的元素。 Sometimes, though... there's still an issue where Selenium does not 'see' elements described with an Xpath while running a script, but when users click the 'Find' button... Selenium has no trouble at all.有时,虽然......仍然存在一个问题,Selenium 在运行脚本时没有“看到”用 Xpath 描述的元素,但是当用户单击“查找”按钮时...... ZC49DFB55F06BB406E2C5CA786F557D 没有问题。 I usually take the focus up a level and down a level before any commands that Selenium has trouble finding the elements for... and it works well thereafter.我通常会在 Selenium 无法找到元素的任何命令之前将焦点提高一个级别,然后再降低一个级别。 It's ugly and very NOT elegant... but it works.它丑陋而且非常不优雅......但它有效。

Selenium uses it's OWN Xpath interpreter... and the one native to your browser might be better in some cases. Selenium 使用它自己的 Xpath 解释器......在某些情况下,浏览器原生的解释器可能会更好。

You can try this instead:你可以试试这个:

WebElement a = driver.findElement(By.xpath("//div[@id=':r6']/span")).getText();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM