简体   繁体   English

如何使用 Selenium Python 在没有“选择”标签的下拉菜单中单击选项?

[英]How to click option in dropdown menu which does not have "select" tag using Selenium Python?

The html code of the dropdown I am concerned with我关注的下拉框的html代码在此处输入图像描述] ]

Here's link to the "Helfulness" dropdown I am talking about.这是我正在谈论的“Helfulness”下拉菜单的链接。 https://play.google.com/store/apps/details?id=com.delta.mobile.android&hl=en https://play.google.com/store/apps/details?id=com.delta.mobile.android&hl=en

After I visit the pagethrough selenium I want to automate the process of selecting the 1st dropdown list and choose option "Newest".在我访问 pagethrough selenium 之后,我想自动执行选择第一个下拉列表的过程并选择选项“最新”。 The problem is the dropdown menu does not have the select tag.Can anyone help?问题是下拉菜单没有 select 标签。有人可以帮忙吗?

尝试使用Selenium IDE来获取下拉菜单的名称,id或xpath。

1. Selenium 1.硒

You can use XPath to find that button by its text: 您可以使用XPath通过其文本查找该按钮:

button = driver.find_element_by_xpath("//button[./text() = 'Helpfulness']")
button.click()

2. Using Capybara (which uses Selenium) 2.使用水豚(使用硒)

More generally (and reliably), you can use capybara-py to click all kinds of buttons (not just <button> elements): 更一般地(可靠地),您可以使用capybara-py单击各种按钮(不仅仅是<button>元素):

page.click_button("Helpfulness")

It also properly escapes your search string and retries if the button is not yet interactable. 如果该按钮尚不可交互,它也可以正确地转义您的搜索字符串并重试。

When we want to select the value from the dropdown which doesn't have select tag , we have build the logic to select it.First step would be clicking on the dropdown and then finding a generic locator for the values of the dropdown. 当我们想从没有select标签的下拉列表中选择值时,我们已经建立了选择逻辑,第一步是单击下拉列表,然后为下拉列表的值查找通用定位符。 Locator should be completely dependent on the value of the dropdown so that just by adding the required value we should be able to get the unique locator , like below Locator应该完全依赖于下拉菜单的值,这样,只需添加所需的值,我们就应该能够获得唯一的locator,如下所示

public void selectValueFromDropDown(WebDriver driver,String value) throws InterruptedException{
        //click on the dropdown
        driver.findElement(By.xpath("//button[@class='dropdown-menu'][1]")).click();
        Thread.sleep(1000);
        // select value from dropdown
        driver.findElement(By.xpath("//div[@class='dropdown-menu-children']//li//button[text()='"+value+"']")).click();
    }

Hope this will help you. 希望这会帮助你。

Hey I have locate this by following code:嘿,我通过以下代码找到了它:

1.WebElement arrow=driver.findElement(By.xpath("(//i[@class='google-material-icons VfPpkd-kBDsod W7A5Qb' and @aria-hidden='true'])[4]")); 1.WebElement arrow=driver.findElement(By.xpath("(//i[@class='google-material-icons VfPpkd-kBDsod W7A5Qb' and @aria-hidden='true'])[4]")) ; // here I have locate developer contact dropdown 2.arrow.click(); //在这里我找到了开发人员联系下拉列表2.arrow.click(); // clicked on it 3.driver.findElement(By.xpath("//a[@href='http://www.delta.com/app']")).click();; //点击它3.driver.findElement(By.xpath("//a[@href='http://www.delta.com/app']")).click();; } **//**located first website from dropdown list using xpath **** } **//** 使用 xpath 从下拉列表中找到第一个网站 ****

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

相关问题 如何使用 Selenium 和 Python 从下拉菜单中选择 select 选项 - How to select an option from the dropdown menu using Selenium and Python 如何使用 Selenium Python 选择下拉菜单 - How to select a dropdown menu using Selenium Python 如何使用Selenium在python中选择下拉菜单 - How to select dropdown menu in python using Selenium 单击下拉选项菜单中的元素 - Selenium Python - click on element in dropdown option menu - Selenium Python 如何从 python selenium 的下拉菜单中单击一个选项? - How can I click on an option from a dropdown menu in python selenium? 如何单击与硒和python不可交互的下拉菜单,然后选择一个选项? - How to click not interactable dropdown with selenium and python and select one option? 如何使用 Selenium 和 Python 从下拉菜单中选择 select 选项 - How to select an option from a dropdown menu through partial text using Selenium and Python 如何使用 Python 在 Selenium 的下拉菜单中打印预选选项的文本? - How to print the text of the preselected option in a dropdown menu with Selenium using Python? 如何使用 Selenium 和 Python 提取下拉菜单的选定选项的文本 - How to extract the text of the selected option of a Dropdown Menu using Selenium and Python 如何选择一个<option>在一个<select>在 Python 3 中使用 Selenium 标记? - How to select an <option> within a <select> tag using Selenium in Python 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM