简体   繁体   English

右键单击-Selenium-Python

[英]Right Click -Selenium - Python

I'm little bit struggling to find the correct way to perform a right click.我有点努力寻找执行右键单击的正确方法。

Here is a sample of my code:这是我的代码示例:

click_Menu = driver.find_element_by_id("Menu")
print(click_Menu.text)
action.move_to_element(click_Menu)
action.context_click(on_element=click_Menu)
action.perform()

All the imports are there.And print(click_Menu.text ) => returns "Menu", so the element has been found所有的导入都在那里。并且print(click_Menu.text ) => 返回“Menu”,所以元素已经找到

Error:错误:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

I tried to add time.sleep() but the result is the same.我试图添加time.sleep()但结果是一样的。

Any ideas please?请问有什么想法吗?

You simply need to pass only the element within context_click() as follows:您只需要传递context_click()中的元素,如下所示:

context_click(click_Menu)

Your effective line of code will be:您的有效代码行将是:

action.context_click(click_Menu)

Optimizing your code block:优化你的代码块:

click_Menu = driver.find_element(By.ID, "Menu")
ActionChains(driver).move_to_element(click_Menu).context_click(click_Menu).perform()

This code will help you to solve the issue.此代码将帮助您解决问题。

from selenium.webdriver import ActionChains

Identifying the source element识别源元素

click_Menu= driver.find_element_by_xpath("your path")

or或者

click_Menu= driver.find_element_by_id("Menu")

Action chain object creation动作链 object 创建

action = ActionChains(driver)

Right-click operation and then perform右键操作然后执行

action.move_to_element(click_Menu).perform()
action.context_click(click_Menu).perform()

The easy way to overcome many of these types of errors is to just add some sort of delay:克服许多此类错误的简单方法是添加某种延迟:

import time
time.sleep(2) 

Solution found.找到解决方案。

I was using an old ActionChains object.我使用的是旧的 ActionChains object。 So i reinstantiate it by creating a new one.所以我通过创建一个新的来重新实例化它。

action1 = ActionChains(driver)

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

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