AttributeError: 'Select' object has no attribute 'get_attribute' error using select_by_index() to select an option from dropdown using Selenium Python
AttributeError: 'Select' object has no attribute 'get_attribute' error using select_by_index() to select an option from dropdown using Selenium Python
我是 python 新手,在从下拉列表中抓取文本时遇到问题 table
这里是问题所在:
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome()
driver.get("https://www.ehail.ca/quotes/?1590984761655")
desired_data =
Select(driver.find_element_by_xpath("/html/body/div[1]/div[2]/form[2]/table[1]/tbody/tr/td/table/tbody[1]/tr[2]/td[10]/select"))
desired_data.select_by_index(1)
print(desired_data.text))
并得到结果错误:
AttributeError: 'Select' object has no attribute 'get_attribute'
我也试过 print(desired_data.text)
但没有成功。
我可以用 selenium 做到这一点吗,或者我需要另一个像 beautiful soup 这样的库吗?
感谢阅读
edit1: 我已经填写了必填字段,但仍然出现同样的错误。我能够 select 元素但是当尝试打印时我得到 <selenium.webdriver.support.select.Select object at 0x087810B8>
您看到的是正确的行为。
在您的程序中,在您调用 Select(webelement)
时调用 get(url)
之后,没有填写之前的必填字段, <option>
项未获得 populated/created:
因此,当您调用 select_by_index(1)
方法时,该方法会在内部调用 if opt.get_attribute("index") == match:
时强制引发错误:
AttributeError: 'Select' object has no attribute 'get_attribute'
def select_by_index(self, index):
"""Select the option at the given index. This is done by examing the "index" attribute of an
element, and not merely by counting.
:Args:
- index - The option at this index will be selected
throws NoSuchElementException If there is no option with specisied index in SELECT
"""
match = str(index)
for opt in self.options:
if opt.get_attribute("index") == match:
self._setSelected(opt)
return
raise NoSuchElementException("Could not locate element with index %d" % index)
解决方案
填写前面的必填字段,将填充 <option>
个元素,程序将成功执行。
在使用.text
方法之前先调用.first_selected_option
方法。
参考文档:
The first selected option in this select tag (or the currently selected option in a normal select)
desired_data.select_by_index(1)
print(desired_data.first_selected_option.text)
它将return当前选择的选项。
我是 python 新手,在从下拉列表中抓取文本时遇到问题 table
这里是问题所在:
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome()
driver.get("https://www.ehail.ca/quotes/?1590984761655")
desired_data =
Select(driver.find_element_by_xpath("/html/body/div[1]/div[2]/form[2]/table[1]/tbody/tr/td/table/tbody[1]/tr[2]/td[10]/select"))
desired_data.select_by_index(1)
print(desired_data.text))
并得到结果错误:
AttributeError: 'Select' object has no attribute 'get_attribute'
我也试过 print(desired_data.text)
但没有成功。
我可以用 selenium 做到这一点吗,或者我需要另一个像 beautiful soup 这样的库吗?
感谢阅读
edit1: 我已经填写了必填字段,但仍然出现同样的错误。我能够 select 元素但是当尝试打印时我得到 <selenium.webdriver.support.select.Select object at 0x087810B8>
您看到的是正确的行为。
在您的程序中,在您调用 Select(webelement)
get(url)
之后,没有填写之前的必填字段, <option>
项未获得 populated/created:
因此,当您调用 select_by_index(1)
方法时,该方法会在内部调用 if opt.get_attribute("index") == match:
时强制引发错误:
AttributeError: 'Select' object has no attribute 'get_attribute'
def select_by_index(self, index):
"""Select the option at the given index. This is done by examing the "index" attribute of an
element, and not merely by counting.
:Args:
- index - The option at this index will be selected
throws NoSuchElementException If there is no option with specisied index in SELECT
"""
match = str(index)
for opt in self.options:
if opt.get_attribute("index") == match:
self._setSelected(opt)
return
raise NoSuchElementException("Could not locate element with index %d" % index)
解决方案
填写前面的必填字段,将填充 <option>
个元素,程序将成功执行。
在使用.text
方法之前先调用.first_selected_option
方法。
参考文档:
The first selected option in this select tag (or the currently selected option in a normal select)
desired_data.select_by_index(1)
print(desired_data.first_selected_option.text)
它将return当前选择的选项。