【问题标题】:Obtaining option tag attributes under select tag without an ID在不带ID的select标签下获取option标签属性
【发布时间】:2020-03-08 06:31:41
【问题描述】:

我需要在一个选择标签下获取一个选项的属性。但是,我无法找到选择标签,因为选择标签没有 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>

我尝试了两种方法

  1. 使用 find_elements_by_tag_name('select') 定位选择标签,然后获取选择元素内的所有文本,然后使用 find_elements_by_xpath ("//option[contains(text(), text)]" 定位选项标签)。找到选项标签后,我可以将 get_attributes 用于所需属性。这不仅看起来很复杂,而且有时也不起作用,因为它没有给出选项文本。

  2. 我尝试通过 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' 未定义”

【问题讨论】:

  • 你想获得 NZ Innings 和 PAK Innings 吗?
  • 是的,我正在尝试获取 NZ 和 PAK 局的评论网址
  • 试试我在回答中提到的xpath

标签: python selenium select css-selectors webdriverwait


【解决方案1】:

试试这个 xpaths:

新西兰局:(.//div[@class='mobile-dropdown']/select/options)[1]

PAK 局:(.//div[@class='mobile-dropdown']/select/options)[2]

【讨论】:

  • first_inn=driver.find_elements_by_xpath(".//span[@class='mobile-arrow']/select/options")[1].get_attribute('data-url')。收到错误“列表索引超出范围”。我犯了语法错误吗?
  • 仍然出现同样的错误。 first_inn=driver.find_elements_by_xpath(".//div[@class='mobile-dropdown']/select/options")[1].get_attribute('data-url')
  • 你能帮忙看看语法吗?我是新手,觉得语法一定有错误。
  • first_inn=driver.find_element_by_xpath(".//span[@class='mobile-arrow']/select/options")[1].get_attribute('data-url') 可以尝试将find_elements 更改为find_element
【解决方案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"))

【讨论】:

  • 我尝试了 XPATH 解决方案,它可以工作。但是,我发现它适用于 print(option.get_attribute("data-url")),但如果我将其分配给像 this-comment_url=option.get_attribute("data-url") 这样的变量,则会引发错误。我需要后者,因为我需要访问 url。为什么是这样?我怎样才能避免错误?
  • 使用 XPATH 解决方案,得到 StaleElementReferenceException。但是,我只为 comment_url=option.get_attribute("data-url") 收到此错误,而不是 print(option.get_attribute("data-url"))
  • 通过分配变量和详细的使用示例检查答案更新
【解决方案3】:

second 选项中提取 &lt;option> 标签属性,例如&lt;option&gt;&lt;select&gt; 标记内将文本作为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"))
    

【讨论】:

  • 两个问题 - 由于我将使用该代码进行许多比赛,因此该代码将没有选项值(例如“PAK innings”)。其次,我需要能够将属性分配给变量而不是打印它。感谢您的帮助。
  • 查看更新的答案并让我知道状态。
猜你喜欢
  • 2022-09-23
  • 2010-12-21
  • 2011-08-11
  • 2013-01-18
  • 2013-03-05
  • 2012-06-10
  • 1970-01-01
  • 2013-10-22
  • 1970-01-01
相关资源
最近更新 更多