python selenium如何单击选项
我想点击这些坏男孩中的一个选项: 如何使代码单击它并使用python selenium如何单击选项,python,selenium,Python,Selenium,我想点击这些坏男孩中的一个选项: 如何使代码单击它并使用value=“196277”选择选项 -Vælg- 伯内尔 达缪尔 戴克鲁 赫鲁尔 洛默尔 智能手表 斯米克库尔 斯托普尔 Uniseur 安第斯山脉 您可以使用xpath查找特定的选项元素: your_choice=browser.find_element_by_xpath("//select/option[@value='196277']") 然后对其调用click()函数: your_choice.click() 这可能对您有用
value=“196277”
选择选项
-Vælg-
伯内尔
达缪尔
戴克鲁
赫鲁尔
洛默尔
智能手表
斯米克库尔
斯托普尔
Uniseur
安第斯山脉
您可以使用xpath查找特定的选项元素:
your_choice=browser.find_element_by_xpath("//select/option[@value='196277']")
然后对其调用click()
函数:
your_choice.click()
这可能对您有用:
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
显然,我在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 当我尝试这样做时,我得到了一个错误消息:xpath一定是不正确的,因为我猜select标记周围也有其他html元素。此页-,将帮助您在页面上找到option元素的特定xpath。之后,点击功能将根据需要工作。我使用chrome。。Firefox实际上不起作用,我只需要睡一觉(2),因为它不起作用的原因是它在加载之前就搜索//select/选项[@value='196277']。这就是为什么它给了我找不到的错误。它没有先装芬兰货!在搜索之前。不知何故,这在FirefoxWebDriver上不起作用,但这不是问题所在^^第2行-驱动程序未定义SeleniumDoesn也将是您的webdriver,我得到的错误是:selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“类名”,“选择器”:“字段媒体”}提供了更多详细信息。如果我们不知道你的密码,我们就帮不了你。该站点可能有一个或多个iframe。这必须首先处理,这不是问题的一部分。你想要完整的html是什么?self等于什么?self是python的东西,请看这里。self.driver是webdriverWelcome堆栈溢出!请阅读。请提供您尝试过的代码和执行结果,包括任何错误消息等。
# -*- 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()