定位元素的八种操作
id 定位
find_element_by_id()
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://login.taobao.com/member/login.jhtml?spm=a21bo.21814703.754894437.1.5af911d9nuqL8v&f=top&redirectURL=https%3A%2F%2Fwww.taobao.com%2F')
cli=driver.find_element_by_id('fm-login-id')
cli.send_keys('888378380')
time.sleep(10)
driver.close()
name 通id定为
find_element_by_name()
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://baidu.com')
inp =driver.find_element_by_name('wd')
inp.send_keys('百度热搜榜')
time.sleep(1.4)
inp.clear()
inp.send_keys('selenium IDE')
driver.find_element_by_id('su').click()
time.sleep(5)
driver.close()
class name 通过class伪类名定为
find_element_by_class_name()
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://bj.58.com/')
inp =driver.find_element_by_class_name('navGrey')
inp.click()
driver.close()
tag name 通过标签定为
find_element_by_tag_name()
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://bj.58.com/')
inp = driver.find_element_by_tag_name('input')
inp.send_keys('北京东城区')
time.sleep(5)
driver.close()
link text 通过链接文本定位
find_element_by_tag_name()
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://bj.58.com/')
inp = driver.find_element_by_link_text('租房')
inp.click()
time.sleep(5)
driver.close()
partial link text 部分链接文本定位
find_element_by_partial_link_text()
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://bj.58.com/')
inp = driver.find_element_by_partial_link_text('出')
inp.click()
time.sleep(5)
driver.close()
xpath 通过xpath路径表达式定位
find_element_by_xpath()
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://bj.58.com/')
inp = driver.find_element_by_xpath(".//*[@id='zpNav']/em/a[3]")
inp.click()
time.sleep(5)
driver.close()
css 通过
find_element_by_css_selector()
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://bj.58.com/')
inp = driver.find_element_by_css_selector(".index_a_red>a")
inp.click()
time.sleep(5)
driver.close()
点击操作
element.click()
inp = driver.find_element_by_css_selector(".index_a_red>a")
inp.click()
time.sleep(5)
高级操作
获取所有窗口的句柄 driver.window_handles()
headre = driver.window_handles()
通过窗口的句柄进入的窗口
driver.switch_to_window(handles[n])
driver.switch_to.window(handles[n])
综合使用,查询58同城房源详情
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://bj.58.com/')
inp = driver.find_element_by_xpath("html/body/div[3]/div[1]/div[1]/div/div[1]/div[1]/span[1]/a")
inp.click()
handles = driver.window_handles
driver.switch_to.window(handles[1])
cli = driver.find_element_by_xpath("html/body/div[5]/div[2]/div[2]/dl[1]/dd/a[4]")
cli.click()
start = driver.find_element_by_xpath(".//*[@id='secitem-rent']/dd/span/span[1]/input")
start.send_keys(1000)
time.sleep(1.2)
end = driver.find_element_by_xpath(".//*[@id='secitem-rent']/dd/span/span[3]/input")
end.send_keys(3000)
time.sleep(1.2)
search = driver.find_element_by_xpath(".//*[@id='secitem-rent']/dd/span/span[5]/a")
search.click()
time.sleep(1.2)
handles = driver.window_handles
driver.switch_to.window(handles[1])
driver.find_element_by_xpath("html/body/div[6]/div[2]/ul/li[1]/div[2]/h2/a").click()
driver.close()
|