python3+selenium寻找元素

打开本地文件

1
2
3
file_path = 'file:///'+os.path.abspath("1.html")
driver = webdriver.Chrome()
driver.get(file_path)

暂停3s

1
2
3
import time
time.sleep(3)

下拉框选择

1
2
3
4
5
from selenium.webdriver.support.ui import Select
s=Select(driver.find_element_by_id("catalogDropdown"))
s.find_element_by_xpath("//option[@value='424636']").click()
s.select_by_visible_text("工作日志")

执行javascript脚本

1
2
js = "document.getElementsByClassName('CodeMirror-activeline')[0].innerHTML='this is test'"
driver.execute_script(js)

切换iframe

1
2
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_tag_name('body').send_keys("hello world!")

根据classname查找

1
driver.find_element_by_class_name("write-btn").click()

根据tagname查找

1
driver.find_element_by_tag_name("input").send_keys("selenium")

通过css方式查找

1
2
3
4
5
6
7
8
driver.find_element_by_css_selector("#kw").send_keys("selenium")
driver.find_element_by_css_selector("a[name='tj_news']").click()
driver.find_element_by_css_selector("a[title='web']").click()
driver.find_element_by_css_selector("a.RecycleBin").click()
driver.findElement(By.cssSelector("#J_login_form>dl>dt>input[id=’ J_password’]"));
#复合样式
#<button id="J_sidebar_login" class="btn btn_big btn_submit" type="submit">登录</button>
driver.findElement(By.cssSelector("button.btn.btn_big.btn_submit"))

浏览器最大化

1
browser.maximize_window()

xpath查找

根据class查找

1
driver.find_element_by_xpath("//div[@class='user-info']/a").click()

根据id查找

1
2
driver.find_element_by_xpath("//div[@id='editorTabList']/a[2]").click()
driver.find_element_by_xpath('//*[@id="account_login"]/form/div/div[5]/button').click()

根据元素位置查找

1
driver.find_element_by_xpath('/html/body/section/div/div[2]/div[2]/div/div[2]/a[4]').click()

根据name查找

1
driver.find_element_by_xpath('//*[@name="title"]')

标签文字

1
2
driver.findElement(By.linkText("About Google"));
driver.findElement(By.partialLinkText("About"));

层级查找元素

xpath

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
27
28
29
#在/form/span/input 层级标签下有个 div 标签的 id=fm 的元素
driver.find_element_by_xpath("//div[@id='fm']/form/span/input").send_keys("s elenium")
# id 为'check' 的 tr ,定位它里面的第2个 td
driver.find_element_by_xpath("//tr[@id='check']/td[2]").click()
#第7个 tr 里面的第2个 td(position)
driver.find_element_by_xpath("//tr[7]/td[2]").click()
#在 a 标签下有个文本(text)包含(contains)'网页' 的元素
driver.find_element_by_xpath("//a[contains(text(),'网页')]").click()
#有个叫 a 的标签,他有个链接 href='http://www.baidu.com/ 的元素
driver.find_element_by_xpath("//a[@href='http://www.baidu.com/']").click()
driver.findElement(By.xpath(“//img[@alt=”div1-img1”]”))
driver.find_element_by_xpath("//div[@name=’div2’]/input[@name=’div2input’]")
driver.findElement(By.xpath("//input[@type='submit'][@name='fuck']"));
driver.findElement(By.xpath("//input[@type='submit' or @name='fuck']"));
driver.findElement(By.xpath("//input[@type='submit' and @name='fuck']"));
#匹配id以fuck开头的元素,id='fuckyou'
WebElement ele = driver.findElement(By.xpath("//input[start-with(@id,'fuck')]"));
#匹配id以fuck结尾的元素,id='youfuck'
WebElement ele = driver.findElement(By.xpath("//input[ends-with(@id,'fuck')]"));
#匹配id中含有fuck的元素,id='youfuckyou'
WebElement ele = driver.findElement(By.xpath("//input[contains(@id,'fuck')]"));
#匹配所有input元素中含有属性的值为fuck的元素
driver.findElement(By.xpath("//input[@*='fuck']"));

根据标签文案查找

1
driver.find_element_by_id('xx').find_element_by_link_text('Another_action')

https://www.cnblogs.com/qingchunjun/p/4208159.html
https://www.cnblogs.com/yufeihlf/p/5689042.html

坚持原创技术分享,您的支持将鼓励我继续创作!