Python+Selenium练习(六)-利用class name定位元素

  很多时候,我们查看元素的XPath信息,发现没有可以用来定位的id信息,这个时候我们就要考虑其他的可用的来定位元素。

练习场景:百度首页的搜索输入框

脚本如下:

# coding=utf-8

from selenium import webdriver

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(6)

driver.get("https://www.baidu.com")
try:
    driver.find_element_by_class_name("s_ipt")
    print('test pass: element found by class name')
except Exception as e:
    print("Exception found",format(e))

driver.quit()

  

总结:很多情况下,class利用要比id多,如果class中出现了太长的字符,和可变化的数字,那么就要用回XPath定位方法。

参考文章:https://blog.csdn.net/u011541946/article/details/68926848

猜你喜欢

转载自www.cnblogs.com/zhaocbbb/p/12621138.html