简体   繁体   English

如何在硒中特定的Web元素内找到即时Web元素?

[英]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.

相关问题 硒中的WebElements与WebElement的区别 - Difference WebElements Vs WebElement in selenium webelement 列表中的一些 wblements 值变为空白如何等待 webelement 的每个值出现在 selenium 中的 webelements 中 - some of the value of weblements in list of webelement are coming blank How to wait for each value of webelement to appear in webelements in selenium Selenium 如何获取 WebElement 中的所有元素 - Selenium how to get all elements inside a WebElement 如何使用具有相同属性的 Selenium 查找多个不同的 Webelement? - How to find multiple different Webelements with Selenium that have the same attribute? Selenium + Java-在页面中找不到WebElements - Selenium+Java - Unable to find WebElements in page 找不到硒WebElement两次 - Can not find selenium WebElement twice 如何使用Selenium在Java中的方法内部初始化另一个WebElement - How to initialize another WebElement inside the method in Java with Selenium Selenium webdriver - 迭代,找到 webelement 然后点击它 - 我该怎么做? - Selenium webdriver - Iterate, find webelement and then click on it - how can I do that? 如何双击Selenium webdriver中的webElement列表 - how to double click on a list of webElements in Selenium webdriver 如何计算动态页面上的 webElements? java, selenium - How to count webElements on dynamic page? java , selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM