Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用Selenium Python选择菜单时出错_Python_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

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

尝试使用Selenium Python选择菜单时出错,python,selenium,selenium-webdriver,selenium-chromedriver,Python,Selenium,Selenium Webdriver,Selenium Chromedriver,我的Selenium IDE在Chrome上录制,打开一个URL,然后点击一个下拉菜单,该菜单似乎会生成一些代码,动态显示3个动作, 打开、单击并将鼠标悬停在命令上。它可以工作,Selenium IDE存储的id、cpath、css_选择器如下所示 { "id": "5f885ad3-990a-4989-9382-2572b2", "version": "2.0", "name": "Test", "url": "https://example.com", "tests": [{ "id

我的Selenium IDE在Chrome上录制,打开一个URL,然后点击一个下拉菜单,该菜单似乎会生成一些代码,动态显示3个动作, 打开、单击并将鼠标悬停在命令上。它可以工作,Selenium IDE存储的id、cpath、css_选择器如下所示

{
"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)

您缺少以下步骤。导航到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();

网页源代码将有助于了解您在中拥有哪些数据selenium@iNoob我在下面的原始问题中附上了与我要单击的按钮相关的代码
UPDATE
。我在源代码中看到许多
id
显示为type=“hidden”。我将您建议的行放在
actionchains
行下面,我得到了与CSS_Selector
NoSuchElementException相同的错误:消息:没有这样的元素:无法定位元素:{“方法”:“CSS Selector”,“Selector”:.blue>.fa”}
它应该在
actionChains=actionChains(driver)
之前,请参阅我在原始帖子中更新的python代码。我之前已经有一个actionChains行用于其他用途(双击)。所以我定义了一个新的actionChains1,在那行之前我放了你建议的那行,但是我得到了同样的错误。我无法将行
…css\u选择器(“.blue>.fa”)。请在第一个actionChains之前单击()
,因为这是一个Begging页面,我要选择的按钮出现在后面。我更新了答案以与IDE步骤相匹配。你现在应该很好了。显然,菜单在第二页。因此必须使用
driver.switch\u到.window(driver.window\u句柄[1])
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();