[英]Drop Down for Selenium and Python
I have the drop-down menu for which the source is as below: 我有一个下拉菜单,其来源如下:
<select name="issuer">
<option selected="selected" value="15">MBBTampereRootCA
</option><option value="66222">OMS_CA1
</option><option value="66225">OMS_CA2
</option><option value="71463">stefanSpiel
</option></select>
and I need to select "stefanSpiel" please tell me how can i do that ?? 我需要选择“ stefanSpiel”,请告诉我该怎么做?
I tried with Multiple possible options, but not successful. 我尝试了多种可能的选择,但未成功。
I have tried these options: 我尝试了以下选项:
browser = webdriver.Firefox()
browser.find_element_by_css_selector("option.stefanSpiel")
browser.find_element_by_link_text('option.stefanSpiel');
and also these: 还有这些:
'element = browser.find_element_by_name("issuer")'
'target = select (option, stefanSpiel)'
'action_chains = ActionChains(browser)'
'action_chains.drag_and_drop(element, target)'
'ActionChains(browser).move_to_element(element).click(target).perform()'
But all i got is : 'selenium.common.exceptions.NoSuchElementException:' 但是我得到的只是:'selenium.common.exceptions.NoSuchElementException:'
Thanks, 谢谢,
One way to do it is by clicking the 'select' element. 一种方法是单击“选择”元素。 This will 'open' the drop-down and make all the drop-down options visible to our driver. 这将“打开”下拉列表,并使所有下拉选项对我们的驱动程序可见。 Now we will need to click the desired element. 现在,我们需要单击所需的元素。
For example, lets take a look at the following html (it's very similar to the html you provided) I took it from http://www.tizag.com/htmlT/htmlselect.php : 例如,让我们看看下面的html(它与您提供的html非常相似)是我从http://www.tizag.com/htmlT/htmlselect.php那里获得的:
<select name="selectionField">
<option value="CA">California -- CA </option>
<option value="CO">Colorado -- CO</option>
<option value="CN">Connecticut -- CN</option>
</select>
For this example I will use xpaths. 对于此示例,我将使用xpaths。 Lets say I have the xpath of the 'select' element: 可以说我有'select'元素的xpath:
xpath = '/html/body/table[3]/tbody/tr[1]/td[2]/table/tbody/tr/td/div[4]/select'
I would like to select the option "Connecticut -- CN" 我想选择“ Connecticut-CN”选项
One way to do it is this: 一种方法是这样的:
from selenium import webdriver
driver = webdriver.Firefox()
# navigate to the page that contains the html I provided
driver.get('http://www.tizag.com/htmlT/htmlselect.php')
# the xpath of the <select> elemnt
xpath = '/html/body/table[3]/tbody/tr[1]/td[2]/table/tbody/tr/td/div[4]/select'
# click on the <select> element to open the dropdown
driver.find_element_by_xpath(xpath).click()
# select the desired option
driver.find_element_by_xpath(xpath+'/*[contains(text(), "Connecticut -- CN")]').click()
I would suggest you use the Select()
class as it exists to handle select
elements. 我建议您使用存在的Select()
类来处理select
元素。
from selenium.webdriver.support.select import Select
select_element = Select(driver.find_element_by_name("issuer"))
select_element.select_by_visible_text("stefanSpiel")
Thanks all... Tried as below and it works... not sure to what extent it is good: 谢谢大家...尝试如下,它可以工作...不确定在什么程度上是好的:
element = browser.find_element_by_name("issuer")
element.send_keys('stefanSpiel' + Keys.RETURN)
Thanks again :) 再次感谢 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.