Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium Javascript-如何找到祖父母和父母_Javascript_Selenium - Fatal编程技术网

Selenium Javascript-如何找到祖父母和父母

Selenium Javascript-如何找到祖父母和父母,javascript,selenium,Javascript,Selenium,所以我一直在试图挑战自己,我一直在试图解决祖父母和父母使用下面的代码时使用的问题 <button do-something="" primary-button="" class="gfjhghjfghjfgfh" disabled=""> <span class="primary-button-wrapper"> <i class="test-object"></i> <span>Nice</span> </

所以我一直在试图挑战自己,我一直在试图解决祖父母和父母使用下面的代码时使用
的问题

<button do-something="" primary-button="" class="gfjhghjfghjfgfh"  disabled="">
<span class="primary-button-wrapper">
  <i class="test-object"></i>
  <span>Nice</span>
</span>

有两个选项,您可以通过它们找到所需元素的父元素。比如说

使用JavascriptExecutor:

//Find elements by executing client side javascript
WebElement myElement = driver.findElement(By.css(".test-project"));
WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript(
                               "return arguments[0].parentNode;", myElement);
使用xpath:

//Locate the element you're treating as a child and move up from it using xpath
WebElement element = driver.findElement(By.css(".test-project"));
WebElement parent = element.findElement(By.xpath("./.."));
WebElement grandParent = element.findElement(By.xpath("./../.."));

有两个选项,您可以通过它们找到所需元素的父元素。比如说

使用JavascriptExecutor:

//Find elements by executing client side javascript
WebElement myElement = driver.findElement(By.css(".test-project"));
WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript(
                               "return arguments[0].parentNode;", myElement);
使用xpath:

//Locate the element you're treating as a child and move up from it using xpath
WebElement element = driver.findElement(By.css(".test-project"));
WebElement parent = element.findElement(By.xpath("./.."));
WebElement grandParent = element.findElement(By.xpath("./../.."));