我想点击其中一个坏男孩的其中一个选项:https://gyazo.com/86a2f8773b182e730fe5c69107efa190
如何让我的代码点击它并选择value="196277"
的选项?
<select name="Input.MatrixElements[0].ValueId" id="matrix-element-666" data-val-required="Produkt skal udfyldes" data-val="true" data-listingtext-format=", {0}" data-listingtext-desc="" data-listingtext-priority="1" data-element-desc="Produkt" class="field-medium" data-matrixid="666">
<option value="">- Vælg -</option>
<option value="115604" data-listingtext-desc="Børneur">Børneur</option>
<option value="17662" data-listingtext-desc="Dameur">Dameur</option>
<option value="17663" data-listingtext-desc="Dykkerur">Dykkerur</option>
<option value="17661" data-listingtext-desc="Herreur">Herreur</option>
<option value="17665" data-listingtext-desc="Lommeur">Lommeur</option>
<option value="245187" data-listingtext-desc="Smartwatch">Smartwatch</option>
<option value="103440" data-listingtext-desc="Smykkeur">Smykkeur</option>
<option value="171750" data-listingtext-desc="Stopur">Stopur</option>
<option value="196277" data-listingtext-desc="Unisexur">Unisexur</option>
<option value="23395" data-listingtext-desc="Andet">Andet</option>
</select>
答案 0 :(得分:1)
您可以使用xpath查找特定的选项元素:
your_choice=browser.find_element_by_xpath("//select/option[@value='196277']")
然后调用它上面的click()
函数:
your_choice.click()
答案 1 :(得分:0)
这可能对您有用:
from selenium.webdriver.support.ui import Select
from selenium import webdriver as driver
menu = Select(driver.find_element_by_id('matrix-element-666'))
for option in menu:
if option.value == '196277':
option.select
else:
pass
答案 2 :(得分:0)
我在stackoverflow或文档中找到的方法都没有工作(仅在FireFox webdriver中^^°)。所以我做了这个解决方法,它在选项的值和选项的文本之间进行转换。然后选项文本将作为键发送,以便在浏览器中选择正确的选项,lol!
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
import time #debugging using your eyes
from captchaUrlToText import CaptchaUrlToText
import getopt
import sys
import tracebackclass
StackSelectOptionScrape(unittest.TestCase):
def setUp(self):
#self.driver = webdriver.Firefox()
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(30)
url ='http://localhost/pysel.html'#location with select/option code
self.base_url = url
self.verificationErrors = []
self.accept_next_alert = True
def test_yadda(self):
driver = self.driver
driver.get(self.base_url)
self.selectOptionByValue('//select','196277',driver)
def selectOptionByValue(self,selectElem,optionValue,driver):
"""Select an option of a drop down menu using selenium webdriver Chrome or FireFox
selecting the text that belongs to a value will always work since the options
always have a unique value and text, otherwise they would not make much sense as
options, would they? =) """
# first get the keys to send
keys = ""
element = driver.find_element_by_xpath(selectElem)
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
value = option.get_attribute("value")
text = option.get_attribute("text")
print("Value is: %s" % value)
print("Text is: %s" % text)
if value == optionValue:
keys = text
# now send keys if menu is popped up
element.click() # make menu pop up
element.send_keys(keys) # send keys to select option (text)
element.click() # select option
time.sleep(3) # verify with your eyes ^^-d
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException, e: return False
return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False
return True
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
我不知道,有时候selenium就像地狱一样,所以如果你正在寻找一个很好的抓取(而不是webtesting)框架,我推荐scrapy.org!