因为测试中遇到要鼠标悬停显示Tooltip,并操作tip上的内容,现记录如下。
方法一:通过鼠标链式操作
- from selenium.webdriver.common.action_chains import ActionChains as AC
-
- ac = AC(self.driver)
-
- lst = self.driver.find_element_by_xpath('//*[@id="workorderdetail"]/div/div/div[2]/div[1]/div/div[5]/div[1]/div[2]/div')
- bt = lst.find_element_by_xpath('./div/div[3]/div/div[@class="tip-function-button"]/svg')
- # 方法一:
- ac.move_to_element(lst).pause(3).click(bt).release(bt).perform() # 鼠标链式操作
方法二:鼠标悬停后点击
- from selenium.webdriver.common.action_chains import ActionChains as AC
-
- ac = AC(self.driver)
- lst = self.driver.find_element_by_xpath('//*[@id="workorderdetail"]/div/div/div[2]/div[1]/div/div[5]/div[1]/div[2]/div')
- bt = lst.find_element_by_xpath('./div/div[3]/div/div[@class="tip-function-button"]/svg')
-
- ac.move_to_element(lst).perform() # 鼠标悬停
- time.sleep(1)
- # 方法二:
- bt.click() # 点击Tooltip上的元素
方法三:鼠标悬停后调用JS点击
- from selenium.webdriver.common.action_chains import ActionChains as AC
-
- ac = AC(self.driver)
-
- lst = self.driver.find_element_by_xpath('//*[@id="workorderdetail"]/div/div/div[2]/div[1]/div/div[5]/div[1]/div[2]/div')
- ac.move_to_element(lst).perform() # 鼠标悬停
- time.sleep(1)
- # 方法三:
- # 通过JS点击Tooltip上的元素
- self.workorder.js_execute("document.querySelector('.tip-function-button').click()")