选择硒中已知元素的父元素
我有一个可以通过 Selenium 1选择的元素.
I have a certain element that I can select with Selenium 1.
不幸的是,我需要单击父元素以获得所需的行为.我可以轻松找到的元素具有无法选择的属性,使其无法点击.如何使用 XPath 向上导航?
Unfortunately I need to click the parent element to get the desired behaviour. The element I can easily locate has attribute unselectable, making it dead for clicking. How do I navigate upwards with XPath?
有两个选项.示例代码是Java语言,但是到其他语言的移植应该很简单.
There are a couple of options there. The sample code is in Java, but a port to other languages should be straightforward.
WebElement myElement = driver.findElement(By.id("myDiv"));
WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript(
"return arguments[0].parentNode;", myElement);
XPath:
WebElement myElement = driver.findElement(By.id("myDiv"));
WebElement parent = myElement.findElement(By.xpath("./.."));
从WebElement
获取驱动程序
注意:如您所见,对于JavaScript版本,您需要 driver
.如果您无权直接访问它,则可以使用以下命令从WebElement
检索它:
Obtaining the driver from the WebElement
Note: As you can see, for the JavaScript version you'll need the driver
. If you don't have direct access to it, you can retrieve it from the WebElement
using:
WebDriver driver = ((WrapsDriver) myElement).getWrappedDriver();