网页上有些元素是支持右键来触发新的菜单的。

  练习场景:在百度首页,百度logo的右键,查看图片。

  场景拆分:

  1.打开百度首页,找到logo,右键鼠标

  2.移动菜单,查看图像,然后点击

  核心问题:如何操作邮件?在Selenium中有一个ActionChains模块支持,右键,鼠标悬停,拖拽,双击等动作。我们可以通过键盘向下箭头来选择查看图像这个菜单,然后点击就可以达到目的。

  具体代码:

# coding=utf-8
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.baidu.com/')
time.sleep(2)

element = driver.find_element_by_xpath("//*[@title='点击一下,了解更多']")
actionChains = ActionChains(driver)
actionChains.context_click(element).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()
print('右键成功')

  

总结:ActionChains下相关方法当前的firefox不工作,这是一个已知bug

 

相关文章:

  • 2021-12-13
  • 2022-12-23
  • 2021-12-12
  • 2021-11-09
  • 2021-12-27
  • 2021-07-24
猜你喜欢
  • 2022-12-23
  • 2021-12-22
  • 2021-11-27
  • 2022-03-12
  • 2021-12-29
  • 2021-12-26
  • 2022-12-23
相关资源
相似解决方案