python selenium无法选择元素

时间:2016-08-04 08:52:16

标签: selenium selenium-webdriver

我试图在网页中选择以下元素。

.active > b:nth-child(1)

这是我的代码:

timbro = browser.find_element_by_css_selector('.active > b:nth-child(1)')
hover = ActionChains(browser).move_to_element(timbro)
hover.perform()

这是我得到的错误的一部分:

  

消息:无法找到元素:   {"方法":" css选择器","选择器":" .active> B:第n个孩子(1)"}"

我尝试做的是激活下拉菜单,以便点击其他链接。

HTML代码段

<li>
    <a href="#nogo" class="main-link active"><b>Menu iniziale</b></a>
    <ul style="display: block;" class="sub-links">
        <li>
            <a href="#nogo" onclick="Esegui('anagrafica.php')">Anagrafica</a>
        </li>
        <li>
            <a href="#nogo" onclick="Esegui('logout.php')">Fine sessione</a>
        </li>
        <li>
            <a href="#nogo" onclick="Esegui('main.php')">Home</a>
        </li>
        <li>
            <a href="#nogo" onclick="Esegui('timbraWFA.php')">Timbro</a>
        </li>
    </ul>
</li>

1 个答案:

答案 0 :(得分:0)

您应该尝试使用WebDriverWait等待元素的存在,如下所示: -

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


wait = WebDriverWait(browser, 5) 

menuIniziale = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Menu iniziale")))

hover = ActionChains(browser).move_to_element(menuIniziale).move_to_element(wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Timbro"))))
hover.click().build().perform()

Edited1 : - 如果遗憾的是LINK_TEXT无效,请尝试使用XPATH,如下所示: -

menuIniziale = wait.until(EC.presence_of_element_located((By.XPATH, ".//a[contains(.,'Menu iniziale')]")))

hover = ActionChains(browser).move_to_element(menuIniziale).move_to_element(wait.until(EC.presence_of_element_located((By.XPATH, ".//a[contains(.,'Timbro')]"))))
hover.click().build().perform()

或尝试使用CSS_SELECTOR,如下所示: -

menuIniziale = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "a.main-link")))

hover = ActionChains(browser).move_to_element(menuIniziale).move_to_element(wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "ul.sub-links > li:nth-child(4)"))))
hover.click().build().perform()

Edited2 :如果此元素位于iframe内,则需要在找到元素之前切换iframe,如下所示: -

wait.until(EC.frame_to_be_available_and_switch_to_it(("frame name or id")))

#Now after successfully switching to frame do any one of the above steps

Edited3 : - 如果您现在可以打开菜单但无法选择subMenu,请尝试以下操作: -

wait.until(EC.frame_to_be_available_and_switch_to_it(("frame name or id")))

menuIniziale = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "a.main-link")))

hover = ActionChains(browser).move_to_element(menuIniziale)
hover.build().perform()

subMenu = wait.until(EC.visibility_of_element_located((By.XPATH, ".//a[contains(.,'Timbro')]")))
subMenu.click()

Edited4 :最终代码

menuIniziale = wait.until(EC.presence_of_element_located((By.XPATH, ".//a[contains(.,'Menu iniziale')]")))
hover = ActionChains(browser).move_to_element(menuIniziale)
hover.perform()

subMenu = wait.until(EC.visibility_of_element_located((By.XPATH, ".//a[contains(.,'Timbro')]")))
subMenu.click()
hover = ActionChains(browser).move_to_element(subMenu)
hover.click().perform()