简体   繁体   中英

Error when trying to select menu with Selenium-Python

My Selenium IDE recording on Chrome that opens a URL and then do click on a dropdown menu that seems to generate some code dinamically shows 3 actions, Open, Click, and Mouse Over commands. It works and the id, cpath, css_selector stored by Selenium IDE are shown below.

{
"id": "5f885ad3-990a-4989-9382-2572b2",
"version": "2.0",
"name": "Test",
"url": "https://example.com",
"tests": [{
    "id": "00fe2ec5-3529-44ef-9367-b5a7fbf",
    "name": "Test",
    "commands": [{
    "id": "c174b4f2-3a55-4f41-954c-22a8e04",
    "comment": "",
    "command": "open",
    "target": "https://example.com",
    "targets": [],
    "value": ""
    }, {
    "id": "763f8999-7973-48fb-864a-fb3965369021",
    "comment": "",
    "command": "click",
    "target": "css=.blue > .fa",
    "targets": [
        ["css=.blue > .fa", "css:finder"],
        ["xpath=//li[@id='MyMenu']/div/button/span[2]", "xpath:idRelative"],
        ["xpath=//li[3]/div/button/span[2]", "xpath:position"]
    ],
    "value": ""
    }, {
    "id": "3c91c590-94a2-44b8-8d17-bb04d3",
    "comment": "",
    "command": "mouseOver",
    "target": "id=myid",
    "targets": [
        ["id=myid", "id"],
        ["css=#myid", "css:finder"],
        ["xpath=//span[@id='myid']", "xpath:attributes"],
        ["xpath=//li[@id='MyMenu']/div/button/span", "xpath:idRelative"],
        ["xpath=//li[3]/div/button/span", "xpath:position"],
        ["xpath=//span[contains(.,'ABC Banner')]", "xpath:innerText"]
    ],
    "value": ""
    }]
}],

}

I've tried the following code in order to reproduce the open menu action but doesn't work

from selenium import webdriver
from time import gmtime, strftime
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome("C:\crd\chromedriver.exe")

driver.get ("https://example.com")

wait = WebDriverWait(driver, 30)
abc = wait.until(EC.element_to_be_clickable((By.XPATH, "//*...")))

actionChains = ActionChains(driver)
actionChains.double_click(abc).perform()

driver.find_element_by_xpath("//*[@id='....']").click()

driver.find_element_by_css_selector(".blue > .fa").click()  ##### Before actionChains1  

actionChains1 = ActionChains(driver)     ### Added new actionChains1
element = driver.find_element_by_id("myid")
actionChains1.move_to_element(element).perform();   

I get this error of no such element like the element is not visible, but actually is visible and website has already loaded completely:

DevTools listening on ws://127.0.0.1:53407/devtools/browser/e4e2207c-bdd6-4754-867c-7b488
Traceback (most recent call last):
File "Script.py", line 49, in <module>
    element=driver.find_element_by_id("myid")
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"myid"}
(Session info: chrome=74.0.3729.131)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 6.1.7601 SP1 x86_64)

How to fix this issue?

Thanks in advance.

UPDATE

Thanks to @supputuri help my issue was fixed. The problem was the page where I was working is a second one that is opened after a login page, then when the new tab was opened, the driver was seeing the first page and the element I was trying to click didn't exist in that page and due to that it was not found.

The line that fixed the issue was adding a switch of window to new page

driver.switch_to.window(driver.window_handles[1])

you are missing the below step. After navigating to the url and before mouse over.

driver.get ("https://example.com")

driver.switch_to.window(driver.window_handles[1])
wait = WebDriverWait(driver, 30)
abc = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,, ".blue > .fa")))
abc.click()

actionChains = ActionChains(driver)
element = driver.find_element_by_id("myid")
actionChains.move_to_element(element).perform();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM