简体   繁体   English

如何在selenium webdriver Python中用鼠标点击第二层元素?

[英]How to click on the 2nd layer of element with mouse in selenium webdriver Python?

I have a code which supposes to 'Mouse Over' the first layer of the element and then click on the second layer of the element which appears when you do 'Mouse Over' action.我有一个代码假设“鼠标悬停”元素的第一层,然后单击执行“鼠标悬停”操作时出现的元素的第二层。 If I execute the code below it always shows me an error 'NoSuchElementException: Message: Unable to locate element: .e'.如果我执行下面的代码,它总是向我显示错误“NoSuchElementException:消息:无法找到元素:.e”。 Please, help to understand what I am doing wrongly.请帮助理解我做错了什么。

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

def select_random_sentence_to_delete(self):
      self.driver = webdriver.Firefox()
      self.driver.get('http://todomvc.com/examples/react/#/')
      action = ActionChains(self.driver);
      firstLevelMenu = self.driver.find_element(By.CLASS_NAME, "view"[2])
      action.move_to_element(firstLevelMenu).perform()
      secondLevelMenu = self.driver.find_element(By.CLASS_NAME, "destroy"[2])
      action.move_to_element(secondLevelMenu).perform()
      secondLevelMenu.click() ```

You need to chain the actions and only after all the actions you perform() :您需要链接操作,并且仅在您执行所有操作之后perform()

driver.get("http://todomvc.com/examples/react/")
for i in range(10):
    WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".new-todo")))
    driver.find_element(By.CSS_SELECTOR, ".new-todo").send_keys(i, Keys.ENTER)
WebDriverWait(driver,30).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".view input")))
for ele in driver.find_elements(By.CSS_SELECTOR, ".view input"):
    ele.click()
    actions = ActionChains(driver)
    actions.move_to_element(ele).move_to_element(driver.find_element(By.CSS_SELECTOR, ".destroy")).click().perform()

Note: I added WebDriverWait so you'll need to import it too.注意:我添加了WebDriverWait ,所以你也需要导入它。

Here are the imports:以下是进口:

from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

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

相关问题 Selenium Webdriver,python-单击页面上的1st / 2nd / 3rd / etc元素? - Selenium Webdriver, python - click 1st/2nd/3rd/etc element on a page? Python Selenium find_element_by_name 跳到第二个元素 - Python Selenium find_element_by_name skip to 2nd element 如何使用 Selenium WebDriver 单击该元素 - How to click that element with Selenium WebDriver 循环迭代时的第二个“元素不可交互异常”,SELENIUM PYTHON - 2nd 'Element not interactable exception' when iterating on the loop, SELENIUM PYTHON 如何在 selenium 中的 python 中访问具有相同 xpath 的第二个元素 - How do I access the 2nd element with the same xpath in python in selenium 如何随机 select 并使用 selenium webdriver(Python)单击元素? - How to randomly select and click on element with selenium webdriver (Python)? 元素未在Odoo中单击python selenium webdriver中的单击 - Element is not clicking on click in python selenium webdriver in Odoo Selenium Webdriver (Python) - 单击 div 元素(复选框) - Selenium Webdriver (Python) - Click on a div element (checkbox) 找到Python Selenium Webdriver元素,但无法单击它 - Python Selenium Webdriver element found but can not click on it 选择第二个<li>选项列表中的元素 Selenium - Select 2nd <li> element in a picklist Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM