我需要在select标签下获取选项的属性。但是,由于选择标签没有ID,我无法找到选择标签。
<div class="dropdown-wrapper">
<div class="mobile-dropdown">
<span class="mobile-arrow"></span>
<select>
<option data-url="/series/17948/commentary/1115802/new-zealand-vs-
pakistan-1st-odi-pak-in-nz-2017-18?innings=1" value="NZ Innings">NZ
Innings
</option>
<option data-url="/series/17948/commentary/1115802/new-zealand-vs-
pakistan-1st-odi-pak-in-nz-2017-18?innings=2" value="PAK
Innings">PAK Innings</option>
</select>
</div>
</div>
我尝试了两种方法
使用find_elements_by_tag_name('select')定位选择标签,然后在select元素中获取所有文本,然后使用find_elements_by_xpath定位选项标签(“ // option [contains(text(),text)]”“ )。找到选项标签后,就可以将get_attributes用作必需的属性。这不仅看起来非常复杂,而且有时也无法使用,因为它没有提供选项文本。
我尝试通过Select(find_element_by_css_selector(“ class”)使用Select。使用的类名称来自div标记。然后使用select.select_by_index(1).getattribute()。但是,我遇到了错误“选择未定义”。
第一个密码
elem=driver.find_elements_by_tag_name('select')
options=[x.text for x in elem]
first_inn=options[2].split('\n')[1]
second_inn=options[2].split('\n')[0]
option=driver.find_elements_by_xpath("//option[contains(text(), first_inn)]")
option[7].get_attribute('data-url')
第二个密码
select = Select(driver.find_element_by_css_selector("mobile-dropdown"))
first_inn=select.select_by_index(1).get_attribute('data-url')
second_inn=select.select_by_index(0).get_attribute('data-url')
对于第一个代码,我得到了['','',''],对于第二个代码,我得到了一条错误消息“未定义名称'Select'”
答案 0 :(得分:0)
尝试使用此xpath:
对于NZ Innings:(.//div[@class='mobile-dropdown']/select/options)[1]
对于PAK Innings:(.//div[@class='mobile-dropdown']/select/options)[2]
答案 1 :(得分:0)
使用用法示例更新:
teams = []
teams_options = driver.find_elements_by_xpath("(//div[@class='mobile-dropdown'])[1]//select//option")
for option in teams_options:
teams.append({"name": option.get_attribute("value"), "url": option.get_attribute("data-url")})
# print teams
print(teams[0].get("name"), teams[0].get("url"))
print(teams[1].get("name"), teams[1].get("url"))
# or
for team in teams:
print(team.get("name"), team.get("url"))
commentaries = {}
commentary_options = driver.find_elements_by_xpath("(//div[@class='mobile-dropdown'])[2]//select//option")
for option in commentary_options:
commentaries[option.get_attribute("value")] = option.get_attribute("data-url")
# print and commentaries dict usage example
print(commentaries.get("Full commentary"))
print(commentaries.get("Wickets"))
print(commentaries.get("Boundary"))
print(commentaries.get("Highlights"))
# print all commentaries
for commentary in commentaries.keys():
print(commentary, commentaries.get(commentary))
使用xpath:
first_select_options = driver.find_elements_by_xpath("(//div[@class='mobile-dropdown'])[1]//select//option")
for option in first_select_options:
print(option.get_attribute("value"), option.get_attribute("data-url"))
second_select_options = driver.find_elements_by_xpath("(//div[@class='mobile-dropdown'])[2]//select//option")
for option in second_select_options:
print(option.get_attribute("value"), option.get_attribute("data-url"))
使用CSS选择器:
selects = driver.find_elements_by_css_selector(".match-commentary select")
for s in selects:
for option in s.find_elements_by_tag_name("option"):
print(option.get_attribute("value"), option.get_attribute("data-url"))
使用WebDriverWait
:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#..
all_selects = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".mobile-dropdown select")))
for option in all_selects[0].find_element_by_tag_name("option"):
print(option.get_attribute("value"), option.get_attribute("data-url"))
for option in all_selects[1].find_element_by_tag_name("option"):
print(option.get_attribute("value"), option.get_attribute("data-url"))
答案 2 :(得分:0)
要从 second 选项中提取<option
>标签属性,例如<option>
在<select>
标签内以 PAK Innings 作为文本,您必须为element_to_be_clickable()
引入 WebDriverWait ,您可以使用以下命令Locator Strategies:
CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.dropdown-wrapper>div.mobile-dropdown select"))).click()
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.dropdown-wrapper>div.mobile-dropdown select option:nth-child(2)"))).get_attribute("data-url"))
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='dropdown-wrapper']/div[@class='mobile-dropdown']//select"))).click()
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='dropdown-wrapper']/div[@class='mobile-dropdown']//select//following-sibling::option[2]"))).get_attribute("data-url"))