简体   繁体   English

硒驱动器-从下拉列表中选择所需的锂项目

[英]Selenium driver - select the desired li item from a drop down list

I am new & trying to learn selenium I am able to select a specific element from the list as I know the xpath: 我是新手并尝试学习硒,因为我知道xpath,所以可以从列表中选择特定元素:

driver.findElement(By.xpath("*[@id='category-dropdown']/descendant::*[@title='Events']"))

Can anyone please tell me how can I select any item from the list as each item will have a different title name? 谁能告诉我如何从列表中选择任何项目,因为每个项目都有不同的标题名称? I just want to select an item by passing the title name as a variable eg select "Jobs" or "Cars & bikes" 我只想通过将标题名称作为变量传递来选择一个项目,例如选择“作业”或“汽车和自行车”

I tried the below code but i am getting the list element size as 0 我尝试了以下代码,但我将列表元素的大小设置为0

List<WebElement> element=driver.findElements(By.xpath(.//*[@id='category-dropdown']/ul/li));
System.out.println(element.size());
for (WebElement webElement : element)
{       
    System.out.println(webElement.getText());
}

the element size is zero 元素大小为零

<div id="category-dropdown" class="drop-layers cate-layer" style="display: block;" data-type="overlay" data-action="focus" data-area="query">
    <ul>
        <li>
            <a href="javascript:void(0);" data-sc_cid="1" data-sc_gid="0" data-sc_cn="all" data-sc_dn="All Categories" title="All Categories">   All Categories </a>
        </li>
        <li>
            <a href="javascript:void(0);" data-sc_cid="1397" data-sc_gid="60" data-sc_cn="cars-bikes" data-sc_dn="Cars & Bikes" title="Cars & Bikes">   Cars & Bikes </a>
        </li>
        <li>
            <a href="javascript:void(0);" data-sc_cid="18224025" data-sc_gid="269" data-sc_cn="mobiles-tablets" data-sc_dn="Mobiles & Tablets" title="Mobiles & Tablets">   Mobiles & Tablets </a>
        </li>
        <li>
           <a href="javascript:void(0);" data-sc_cid="18222212654" data-sc_gid="247" data-sc_cn="electronics-appliances" data-sc_dn="Electronics & Appliances" title="Electronics & Appliances">   Electronics & Appliances </a>
        </li>
        <li>
            <a href="javascript:void(0);" data-sc_cid="1405" data-sc_gid="20" data-sc_cn="real-estate" data-sc_dn="Real Estate" title="Real Estate">   Real Estate </a>
        </li>
        <li>
            <a href="javascript:void(0);" data-sc_cid="1325" data-sc_gid="123" data-sc_cn="services" data-sc_dn="Services" title="Services">   Services </a>

Looking at your locator for the list 查看您的定位器以获取列表

List<WebElement> element=driver.findElements(By.xpath(.//*[@id='category-dropdown']/ul/li));

You've not enclosed the xpath in quotations, so it won't recognise it as valid, it should be 您尚未将xpath括在引号中,因此它不会将其识别为有效,应该是

List<WebElement> element=driver.findElements(By.xpath("//*[@id='category-dropdown']/ul/li"));

Whenever locate an element "" is required. 无论何时找到元素""都是必需的。

For example: 例如:

driver.findElements(By.id("xyz"));
driver.findElements(By.className("xyz"));
driver.findElements(By.xpath("//*[@id='xyz']"));

simply change this statement 只需更改此语句

List<WebElement> element=driver.findElements(By.xpath("//*[@id='category-dropdown']/ul/li"));

rest is fine 休息很好

You can use XPath for that. 您可以为此使用XPath。 When you do not have to click on the dropdown first; 当您不必首先单击下拉列表时;

WebElement el = driver.findElement(By.xpath("//div[@id = 'category-dropdown']/descendant::li[text() = 'Cars @ Bikes']"));
el.click();

When you have to click the dropdown first: 当您必须首先单击下拉列表时:

WebElement dropdown = driver.findElement(By.id("category-dropdown"));
dropdown.click();
dropdown.findElement(By.cssSelector("li[value='Cars & Bikes']")).click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 硒驱动器-在列表中选择所需的li项目 - Selenium driver - select the desired li item in the list li未从Selenium WebDriver的下拉列表中选择列表项 - list item li is not selecting from drop down through Selenium WebDriver 如何使用带有 Java 的 Selenium WebDriver 从下拉列表中选择项目? - How to select item from drop down list using Selenium WebDriver with Java? Selenium Web驱动程序通过从Excel工作表中读取下拉值来从下拉菜单中选择数据 - Selenium Web driver select data from drop down by reading drop down value from excel sheet Selenium - Select 列表中的项目按 ul li 值文本 - Selenium - Select Item From List By The ul li Value Text 无法从Selenium Webdriver的下拉列表中选择隐藏元素 - Not able to select hidden element from drop down list in Selenium Webdriver 无法使用Java从Selenium Webdriver的下拉列表中选择选项 - Not able to select option from drop down list in selenium webdriver with java Select 基于 excel 使用 selenium Z2567A5EC9705EB71AC2C98403E 驱动程序的数据的多个下拉选项 - Select multiple drop down options based on the data from excel using selenium web driver 无法在Goibibo网站的Selenium Web Driver下拉列表中选择值 - Unable to select the value in the drop down in selenium Web Driver for the website Goibibo 如何在Selenium Web驱动程序中选择下拉框 - how to select the drop down box in selenium web driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM