标签:form 移动 coding 无法定位 click class visible code utf-8
firefox版本:61
当点击“设置”元素后,是无法定位的,无法定位代码如下:
(当时这个对于火狐无法定位的代码,Chrome浏览器是可以正常使用的)
1 from selenium import webdriver 2 from time import sleep 3 4 driver=webdriver.Firefox() 5 driver.get("https://www.baidu.com") 6 7 driver.find_element_by_link_text("设置").click() 8 sleep(2) 9 driver.find_element_by_link_text("搜索设置").click() 10 sleep(3) 11 12 driver.find_element_by_link_text("保存设置").click() 13 14 15 alert=driver.switch_to_alert 16 print(alert.text) 17 alert.accept() 18 sleep(2) 19 20 driver.quit()
针对点击元素修改为鼠标悬停后,可正常定位:
from selenium import webdriver from time import sleep from selenium.webdriver.common.action_chains import ActionChains driver=webdriver.Firefox() driver.get("https://www.baidu.com") mouse=driver.find_element_by_link_text("设置") ActionChains(driver).move_to_element(mouse).perform() sleep(2) driver.find_element_by_link_text("搜索设置").click() sleep(3) driver.find_element_by_link_text("保存设置").click() alert=driver.switch_to_alert print(alert.text) alert.accept() sleep(2) driver.quit()
在后面添加一个最终版的代码:
# coding:utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains#鼠标操作 from selenium.webdriver.support.select import Select#下拉框 import time driver = webdriver.Firefox() url = "https://www.baidu.com" driver.get(url)#打开百度主页 driver.implicitly_wait(20)#隐式等待20s # 鼠标移动到“设置”按钮 mouse = driver.find_element_by_link_text("设置")#定位到“设置” ActionChains(driver).move_to_element(mouse).perform()#鼠标悬停 driver.find_element_by_link_text("搜索设置").click()#定位到“搜索设置”后点击 # 通过text:select_by_visible_text() s = driver.find_element_by_id("nr") Select(s).select_by_visible_text("每页显示20条") time.sleep(3) s.click() driver.find_element_by_link_text("保存设置").click() time.sleep(5) # 获取alert弹框 t = driver.switch_to_alert() print(t.text) t.accept()
标签:form 移动 coding 无法定位 click class visible code utf-8
原文地址:https://www.cnblogs.com/Testcase/p/9498217.html