我正在使用Selenium Web驱动程序进行Webscrap。我已经完成了一个选项值,但我与选项文本有关。我必须 scrape 选项文本,然后传递给your_choice=driver.find_element_by_xpath("//select/option[@value = {}]".format(b))
。以下是代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
driver = webdriver.Firefox(executable_path='./geckodriver')
url = 'https://ipr.etsi.org'
driver.get(url)
button = driver.find_element_by_id(("btnConfirm"))
button.click()
select_element = Select(driver.find_element_by_name("ctl00$cphMain$lstCompany"))
data = []
for option in select_element.options:
data.append(option.get_attribute('value'))
for a in data:
b = a
your_choice = driver.find_element_by_xpath("//select/option[@value = {}]".format(b))
# continue
your_choice.click()
python_button = driver.find_element_by_id(("ctl00_cphMain_btnSearch"))
python_button.click()
上面是我使用该选项值的代码。现在我必须使用选项文本进行。
,因为您正在通过文本选择。
更改option.get_attribute('value'(到
data.append(option.text) #this will add 'Optis Cellular Technology','Orange',etc to data
和"//select/option [@value = {}]" to
if a != '': #to skip the first option because it's empty
your_choice = driver.find_element_by_xpath("//select/option[text()='{0}']".format(a))
your_choice.click()
如果要通过可见文本选择,则python
具有select_by_visible_text
方法。您需要做的就是这样捕获option
元素:
select = Select(driver.find_element_by_id("option_id"))
或通过您选择的任何其他选择器,然后使用该功能:
select.select_by_visible_text("visible_text")