繁体   English   中英

尝试使用Selenium-Python选择菜单时出错

[英]Error when trying to select menu with Selenium-Python

我在Chrome上录制的Selenium IDE会打开一个URL,然后单击一个似乎会生成一些代码的下拉菜单,从而显示了3个动作,分别是“打开”,“单击”和“鼠标悬停”命令。 它可以正常工作,Selenium IDE存储的id,cpath,css_selector如下所示。

{
"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": ""
    }]
}],

}

我尝试了以下代码来重现打开菜单的操作,但不起作用

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

我收到此错误,因为没有这样的元素,例如该元素不可见,但实际上是可见的,并且网站已经完全加载:

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)

如何解决这个问题?

提前致谢。

UPDATE

感谢@supputuri帮助,我的问题已解决。 问题是我正在工作的页面是登录页面之后打开的第二个页面,然后当打开新选项卡时,驱动程序看到的是第一页,而我尝试单击的元素不存在页面,由于找不到该页面。

解决该问题的行是将窗口切换添加到新页面

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

您缺少以下步骤。 导航到url之后,然后将鼠标悬停在上方。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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