尝试使用此xpath:
对于NZ Innings:(.//div[@class='mobile-dropdown']/select/options)[1]
对于PAK Innings:(.//div[@class='mobile-dropdown']/select/options)[2]
,
使用用法示例更新:
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"))
,
要从 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"))
本文链接:https://www.f2er.com/3116761.html