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

时间:2019-05-14 22:08:43

标签: python selenium selenium-webdriver selenium-chromedriver

我在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)

如何解决此问题?

谢谢。

更新

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

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

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

1 个答案:

答案 0 :(得分:1)

您缺少以下步骤。导航到该网址之后,然后将鼠标悬停在上面。

contramap (flip (foldl (flip f)) [x1,x2,...,xn]) z