TA的每日心情 | 慵懒 2017-5-25 13:57 |
---|
签到天数: 29 天 连续签到: 1 天 [LV.4]测试营长
|
本帖最后由 若希晴 于 2016-8-31 09:46 编辑
新手上路~~求问如何使用python+selenium定位隐藏的页面元素
如:定位【百度首页】-【设置】-【搜索设置】
代码:
#coding:utf-8
from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.implicitly_wait(30)
driver.get('https://www.baidu.com/')
driver.maximize_window()
driver.find_element_by_link_text(u"设置").click()
time.sleep(5)
driver.find_element_by_xpath("/html/body/div[3]/div[5]/a[1]").click()
driver.quit()
报错:
Traceback (most recent call last):
File "F:/hello.py", line 12, in <module>
driver.find_element_by_xpath("/html/body/div[3]/div[5]/a[1]").click()
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 72, in click
self._execute(Command.CLICK_ELEMENT)
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 461, in _execute
return self._parent.execute(command, params)
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
ElementNotVisibleException: Message: element not visible
(Session info: chrome=51.0.2704.106)
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86)
之后修改成:
#coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver=webdriver.Chrome()
driver.implicitly_wait(30)
driver.get('https://www.baidu.com/')
driver.maximize_window()
ele=driver.find_element_by_link_text(u"设置")
ActionChains(driver).move_to_element(ele).perform()
time.sleep(5)
driver.find_element_by_xpath("/html/body/div[3]/div[5]/a[1]").click()
time.sleep(5)
driver.quit()
第一次运行未报错,第二次运行仍出现和上面一样的错误
求大神指点~~~ |
|