Python 在“无ID选择标记”下获取选项标记属性
我需要获取select标记下的选项属性。但是,我无法找到select标记,因为select标记没有idPython 在“无ID选择标记”下获取选项标记属性,python,selenium,select,css-selectors,webdriverwait,Python,Selenium,Select,Css Selectors,Webdriverwait,我需要获取select标记下的选项属性。但是,我无法找到select标记,因为select标记没有id <div class="dropdown-wrapper"> <div class="mobile-dropdown"> <span class="mobile-arrow"></span> <select> <option data-url="/series/17948/commentar
<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>
第二代码
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')
对于第一个代码,我得到[“”,“”],对于第二个代码,我得到一条错误消息“name'Select'未定义”尝试使用此XPath:
新西兰局:(../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"))
要从第二个选项中提取标记属性,例如
,在
标记中,文本为PAK Innings,您必须将元素的WebDriverWait诱导为可点击()
,并且可以使用以下方法:
- CSS_选择器:
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"))
你想得到新西兰和巴基斯坦的局数吗?是的,我想用我在回答中提到的xpath
获取新西兰和巴基斯坦局数的评论的url。first\u inn=driver。通过xpath(“.//span[@mobile-arrow']/select/options”)查找元素[1]。获取属性('data-url')。获取错误“列表索引超出范围”。我是不是犯了语法错误?还是同样的错误。first_inn=driver.find_elements_by_xpath(“.//div[@class='mobile-dropdown']/select/options”)[1]。获取属性('data-url')可以帮助您理解语法。我是新手,觉得语法一定有错误。first\u inn=driver.find\u element\u by\u xpath(“.//span[@class='mobile-arrow']/select/options”)[1]。get\u attribute('data-url')
你能试着把find\u elements
改成find\u element
我试过xpath解决方案,效果不错。但是,我发现它适用于打印(option.get_属性(“数据url”)),但如果我将其分配给这样的变量-comment_url=option.get_属性(“数据url”),则会抛出一个错误。我需要后者,因为我需要访问url。为什么会这样?如何避免错误?使用XPATH解决方案,获取StaleElementReferenceException。但是,我得到这个错误仅仅是因为注释_url=option。获取_属性(“数据url”)不是为了打印(option.get _属性(“数据url”))通过分配变量和详细用法检查答案更新示例两个问题-由于我将在许多匹配中使用代码,代码将没有选项值(例如“PAK innings”)。其次,我需要能够将属性分配给变量,而不是打印它。谢谢你的帮助。请查看更新的答案并让我知道状态。
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"))