[英]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。
You can use XPath to find that button by its text: 您可以使用XPath通过其文本查找该按钮:
button = driver.find_element_by_xpath("//button[./text() = 'Helpfulness']")
button.click()
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.