selenium定位隐藏元素

通过编写js代码更改隐藏元素的display属性,然后执行即可,详见代码

import time
from selenium import webdriver

driver = webdriver.Chrome('/home/python/Desktop/spider/driver/chromedriver')
driver.get("http://www.itcast.cn/")

js = "document.getElementsByClassName('city_con')[0].style.display='block'"
driver.execute_script(js)

a = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/div[1]/div/div/div/ul/li[17]/a/span[2]')
print(a.text)

time.sleep(3)
driver.quit()

你可能感兴趣的:(selenium定位隐藏元素)