使用python硒选择隐藏的<li> </li>

时间:2019-01-21 02:18:52

标签: python selenium

我有这么多源html代码:

<div class="timezone" id="js-timezone">
  <span class="timezone__current">2:27 (GMT +2)</span>
  <ul class="timezone__list">
    <li><a href="javascript:void(0);"  onclick="set_timezone('+3');"><span>+3</span>Moscow, Riyadh</a></li>
    <li><a href="javascript:void(0);" onclick="set_timezone('+4');"><span>+4</span>Muscat</a>     </li>
  </ul>
</div>

当我将光标移动到class =“ timezone__current”时,它是隐藏的,显示菜单。 现在,我尝试使用Python和Selenium单击“ +3 GMT”

print ('Opening browser...')
driver = webdriver.Firefox(executable_path = 'C:\geckodriver\geckodriver.exe')
driver.get("https://www.betexplorer.com/next/soccer/")
search_timezone = 
driver.find_element_by_xpath("//ul[@class='timezone__list']")
driver.execute_script('arguments[0].setAttribute("onclick", "set_timezone(+3);")', search_timezone)

什么也没发生。

我也尝试过:

driver = webdriver.Firefox(executable_path = 'C:\geckodriver\geckodriver.exe')
driver.get("https://www.betexplorer.com/next/soccer/")
search_timezone = 
driver.find_element_by_xpath("//ul[@class='timezone__list']")
wait = WebDriverWait(driver, 10)
action = ActionChains(driver)
action.move_to_element(search_timezone).perform()
select_timezone = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "+3 Moscow, Riyadh")))
select_timezone.click()

还有TimeoutException

请帮我,我快疯了

1 个答案:

答案 0 :(得分:-1)

发现了:

search_timezone = driver.find_element_by_class_name("timezone")
action = ActionChains(driver)
action.move_to_element(search_timezone).perform()
wait = WebDriverWait(driver, 10)
select_timezone = search_timezone.find_elements_by_tag_name('a')[3]
select_timezone.click()