可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using selenium to test my web application and I can successfully find tags using By.xpath
. However now and then I need to find child nodes within that node.
Example:
I can do:
WebElement divA = driver.findElement( By.xpath( "//div[@id='a']" ) )
But now I need to find the input, so I could do:
driver.findElement( By.xpath( "//div[@id='a']//input" ) )
However at that point in code I only have divA
, not its xpath any more... I would like to do something like this:
WebElement input = driver.findElement( divA, By.xpath( "//input" ) );
But such an function does not exist. Can I do this anyhow?
BTW: Sometimes I need to find a DIV
that has a certain decendent node. How can I ask in xpath for "the div-tag that contains a span with the text 'hello world'"?
回答1:
According to JavaDocs, you can do this:
WebElement input = divA.findElement(By.xpath(".//input"));
How can I ask in xpath for "the div-tag that contains a span with the text 'hello world'"?
WebElement elem = driver.findElement(By.xpath("//div[span[text()='hello world']]"));
The XPath spec is a suprisingly good read on this.
回答2:
If you have to wait there is a method presenceOfNestedElementLocatedBy
that takes the "parent" element and a locator, e.g. a By.xpath
:
WebElement subNode = new WebDriverWait(driver,10).until( ExpectedConditions.presenceOfNestedElementLocatedBy( divA, By.xpath(".//div/span") ) );
回答3:
I also found myself in a similar position a couple of weeks ago. You can also do this by creating a custom ElementLocatorFactory (or simply passing in divA into the DefaultElementLocatorFactory) to see if it's a child of the first div - you would then call the appropriate PageFactory initElements method.
In this case if you did the following:
PageFactory.initElements(new DefaultElementLocatorFactory(divA), pageObjectInstance)); // The Page Object instance would then need a WebElement // annotated with something like the xpath above or @FindBy(tagName = "input")
回答4:
The toString() method of Selenium's By-Class produces something like "By.xpath: //XpathFoo"
So you could take a substring starting at the colon with something like this:
String selector = divA.toString().substring(s.indexOf(":") + 2);
With this, you could find your element inside your other element with this:
WebElement input = driver.findElement( By.xpath( selector + "//input" ) );
Advantage: You have to search only once on the actual SUT, so it could give you a bonus in performance.
Disadvantage: Ugly... if you want to search for the parent element with css selectory and use xpath for it's childs, you have to check for types before you concatenate... In this case, Slanec's solution (using findElement on a WebElement) is much better.
回答5:
For Finding All the ChildNodes you can use the below Snippet
List childs = MyCurrentWebElement.findElements(By.xpath("./child::*")); for (WebElement e : childs) { System.out.println(e.getTagName()); }
Note that this will give all the Child Nodes at same level -> Like if you have structure like this :