python selenium unable to select element

青春壹個敷衍的年華 提交于 2021-02-10 05:40:37

问题


I'm trying to select below element in a webpage.

.active > b:nth-child(1)

This is my code :

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

This is part of the error I'm getting:

Message: Unable to locate element: {"method":"css selector","selector":".active > b:nth-child(1)"}"

what I'm trying to do is activate a drop down menu so that I can click on another link.

HTML Snippet:

<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:


You should try using WebDriverWait to wait until presence of element as below :-

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 :- If unfortunately LINK_TEXT does not work try using XPATH as below :-

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()

Or try using CSS_SELECTOR as below :-

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 : If this element is inside an iframe, you need to switch that iframe before finding element as below :-

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 :- If you are now able to open menu but not able to select subMenu try as below :-

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: Final code

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()


来源:https://stackoverflow.com/questions/38762735/python-selenium-unable-to-select-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!