[英]How to find immediate webelements inside particular webelement in selenium?
Below is my html code: 以下是我的html代码:
<div class="abc">
<ul class="a">
<li></li>
<ul class="b">
<ul class="c">
<div class="abcd">
<ul class="d">
<li></li>
I want to locate first ul element 我想找到第一个ul元素
css locator would be = div.abc > ul
CSS定位器将是= div.abc> ul
But I want to locate this element using below code: 但是我想使用下面的代码找到这个元素:
final String firstUlElement = ">ul";
@FindBy(css = ".div.abc")
WebElement divElement;
List<WebElement> firstUlElement = this.divElement.findElements(By.cssSelector(firstUlElement));
But it throws following error: 但它引发以下错误:
The given selector >ul> is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: An invalid or illegal selector was specified
Command duration or timeout: 7 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'PratikPat-w7', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_71'
*** Element info: {Using=css selector, value=>ul}
Session ID: ba99cd84-0640-4af7-870a-50d79d893332
Driver info: org.openqa.selenium.firefox.FirefoxDriver
So how to locate first immediate element after div.abc using above code?? 那么如何使用上述代码在div.abc之后找到第一个立即数元素呢? Thnaks in advance
提前致意
You don't need to add >
when you are using findElements()
on a WebElement
. 在
WebElement
上使用findElements()
时,无需添加>
。 If you make this change it should work. 如果进行此更改,它应该可以工作。
final String firstUlElement = "ul";
Note : You are using findElements()
. 注意 :您正在使用
findElements()
。 This will result in a list of elements and not just the first ul
. 这将导致元素列表,而不仅仅是第一个
ul
。 Don't you mean to use findElement()
? 您不是要使用
findElement()
吗?
divElement.findElements(By.cssSelector(firstUlElement));
will search for tag >ul
under the divElement
. 将在
divElement
下搜索>ul
标记。 There isn't such tag. 没有这样的标签。
To search for immediate child using >
you need to use the parent and identifiers together in the cssSelector
要使用
>
搜索直系子代,您需要在cssSelector
一起使用父代和标识符
By.cssSelector(div.abc > ul)
Or using the divElement
或使用
divElement
final String firstUlElement = "ul";
@FindBy(css = "div.abc")
WebElement divElement;
WebElement firstElement = divElement.findElement(By.cssSelector(firstUlElement));
This will give you the first element it finds. 这将为您提供它发现的第一个元素。
Note I removed the dot from div.abc
. 注意我从
div.abc
删除了该点。 Dot represents a class, you can't use it on <div>
tag. 点代表一个类,您不能在
<div>
标签上使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.