的webdriver。蟒蛇。我希望按值将下拉菜单中的一个选项存储为文本,怎么做?

时间:2014-01-29 17:49:01

标签: python html selenium selenium-webdriver

我希望按值将下拉菜单中的一个选项存储为文本。我通过下一个Python脚本选择随机选项:

#Random select option by value
assignJob = Select(driver.find_element_by_name('job[job_title]'))
jobValue = str(randint(1, 6))
assignJob.select_by_value(jobValue)

HTML code:

<select name="job[job_title]" class="formSelect valid" id="job_job_title">
  <option value="" selected="selected">-- Select --</option>
  <option value="1">Customer Service</option>
  <option value="4">QA Engineer</option>
  <option value="3">QA Manager</option>
  <option value="2">SDET</option>
  <option value="5">Software Developer</option>
  <option value="6">Software Development Manager</option>
</select>

当我分配变量storedJob = driver.find_element_by_css_selector(“#job_job_title option [value = jobValue]”)时出错.text:

Traceback (most recent call last):
  File "code.py", line 62, in <module>
    storedJob = driver.find_element_by_css_selector("#job_job_title option[value
=jobValue]").text
  File "C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriv
er\remote\webdriver.py", line 365, in find_element_by_css_selector
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
  File "C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriv
er\remote\webdriver.py", line 681, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriv
er\remote\webdriver.py", line 164, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriv
er\remote\errorhandler.py", line 164, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'no such element\n
 (Session info: chrome=32.0.1700.102)\n  (Driver info: chromedriver=2.8.241075,p
latform=Windows NT 6.1 SP1 x86)'

代码:

assignJob = Select(driver.find_element_by_name('job[job_title]'))
jobValue = str(randint(1, 6))
assignJob.select_by_value(jobValue)
storedJob = driver.find_element_by_css_selector("#job_job_title option[value=jobValue]").text
print storedJob

另一个错误:

Traceback (most recent call last):
  File "code.py", line 63, in <module>
    storedJob = driver.find_element_by_css_selector(jobValueSelector).text
  File "C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriv
er\remote\webdriver.py", line 365, in find_element_by_css_selector
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
  File "C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriv
er\remote\webdriver.py", line 681, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriv
er\remote\webdriver.py", line 164, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriv
er\remote\errorhandler.py", line 164, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidElementStateException: Message: u"invalid elem
ent state: Failed to execute query: '#job_job_title option[value=2]' is not a va
lid selector.\n  (Session info: chrome=32.0.1700.102)\n  (Driver info: chromedri
ver=2.8.241075,platform=Windows NT 6.1 SP1 x86)"

2 个答案:

答案 0 :(得分:2)

您可以使用CSS选择器找到该选项并让selenium返回其文本。

driver.find_element_by_css_selector("#job_job_title option[value=2]").text

2替换为您特定方案的变量jobValue

修改

jobValue是一个变量(str),你不能直接在Python中传递一个变量。

jobValueSelector = "#job_job_title option[value='%s']" %jobValue
storedJob = driver.find_element_by_css_selector(jobValueSelector).text
print storedJob

答案 1 :(得分:1)

下面的代码是Ruby。我认为Python和Ruby几乎是一样的。

select_list = driver.find_element(:id, 'job_job_title')
options = select_list.find_elements(:tag_name, 'option')
index = rand(1..options.count) # Get random number from index 1 to options.count
my_variable =  options[index]

让我尝试使用Python的技巧(未经测试)! :)

select_list = driver.find_element_by_id("job_job_title")
options = select_list.find_elements_by_tag_name("option")
index = randint(1, len(options)) 
variable = options[index]