selenium元素定位

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# coding=utf-8
from selenium import webdriver
driver = webdriver.Ie()
driver.get("https://www.baidu.com")
# driver.find_element_by_link_text("知乎").click()
"""
find_element_by_xpath xpath定位 
find_element_by_id ID定位
find_element_by_class_name class定位
find_element_by_tag_name 标签定位
find_element_by_css_selector css定位
.class #id * tag tag>tag tag+tag [target=_black]
css                                  xpath
div                                  //div  标签
#id                                  //div[@id='']
.class                               //div[@class='']
div[title^=/$=/*=/='']               //div[title='']
div(#/.)>h1                          //div[@id='']/* 
"""
driver.find_element_by_xpath(
    "/html/body/div[1]/div[1]/div/div[1]/div/form/span[1]/input").send_keys("mbg17.cn")
#driver.find_element_by_xpath(
#    "/html/body/div[1]/div[2]/div/div[1]/div/form/span[@class='bg s_btn_wr']/input[@id='su' and @class='bg s_btn']").click()
driver.find_element_by_css_selector("form#form>span>input#su")
print(driver.title)
driver.quit()
北关漫步

北关漫步

咸鱼....

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>