简体   繁体   中英

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'. 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() :

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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