【问题标题】:Error when trying to select menu with Selenium-Python尝试使用 Selenium-Python 选择菜单时出错
【发布时间】:2019-10-02 00:05:26
【问题描述】:

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

【问题讨论】:

  • 网页源代码有助于了解您在 selenium 中拥有哪些数据
  • @iNoob 我在原始问题的UPDATE 下方附上了与我想要单击的按钮相关的代码。我看到很多id's在源代码中显示为type="hidden"。

标签: python selenium selenium-webdriver selenium-chromedriver


【解决方案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();

【讨论】:

  • 我已将您建议的行放在actionchains 行下方,CSS_Selector NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".blue &gt; .fa"} 出现相同的错误
  • 应该在actionChains = ActionChains(driver)之前
  • 请在原帖中查看我更新的python代码。我之前已经有一个用于其他目的的 actionChains 行(双击)。所以我定义了我新的 actionChains1 并在该行之前放置了你建议的行,但我得到了同样的错误。我不能把...css_selector(".blue &gt; .fa").click() 放在第一个actionChains 之前,因为那是一个开始页面,我想选择的按钮出现在后面。
  • 我更新了答案以匹配您的 IDE 步骤。你现在应该很好了。
  • 显然,菜单在第二页。所以必须使用driver.switch_to.window(driver.window_handles[1])
猜你喜欢
  • 2022-11-06
  • 2022-08-13
  • 1970-01-01
  • 2020-07-30
  • 2021-08-30
  • 2017-06-25
  • 2023-03-25
  • 2022-11-11
  • 2021-11-04
相关资源
最近更新 更多