webdriver.Chrome(executable_path='/path/to/chromedriver') # 打开目标网页 driver.get('http://example.com') # 使用XPath查找所有的标签 paragraphs = driver.find_elements_by_xpath('//p') # 遍历所有的标签,并打印其文本内容 for p in paragraphs: print(p.text) # 关闭浏览器 driver.quit() ...
driver.get('D:\\py\\AutoTestFramework\\src\\others\\test.html')# 1.串联寻找printdriver.find_element_by_id('B').find_element_by_tag_name('div').text# 2.xpath父子关系寻找printdriver.find_element_by_xpath("//div[@id='B']/div").text# 3.css selector父子关系寻找printdriver.find_elem...
get("http://localhost") # 3. 点击登录 : 使用xpath属性定位 driver.find_element_by_xpath("//a[@href='/Home/user/login.html']").click() time.sleep(3) # 输入用户名,密码,验证码 :使用逻辑与属性集合定位 driver.find_element_by_xpath("//input[@class='text_cmu' and @name='username']...
# a=html.xpath('//body//a[contains(@class,"li") and @name="items"]/text()')# 10 按序选择# a=html.xpath('//a[2]/text()')# a=html.xpath('//a[3]/@href')# 取最后一个# a=html.xpath('//a[last()]/@href')# 位置小于3的# a=html.xpath('//a[position()<3]/@href'...
from selenium import webdriver driver = webdriver.Firefox() driver.get('D:\\py\\AutoTestFramework\\src\\others\\test.html') # 1.串联寻找 print driver.find_element_by_id('B').find_element_by_tag_name('div').text # 2.xpath父子关系寻找 ...
.get("https://www.baidu.com/") driver.maximize_window() time.sleep(3) # 定位百度搜索框 driver.find_element_by_id("kw").send_keys("python") time.sleep(3) driver.find_element_by_id("su").click() time.sleep(5) # 找到这个元素 ele = driver.find_element_by_xpath('//a[text()=...
(⽗层级定位策略/目标元素定位策略) 代码实现: # 导包 from time import sleep from selenium import webdriver # 实例化浏览器对象 driver = webdriver.Chrome() # 打开网址url driver.get('https://www.baidu.com/') # 需求 # 1、相对路径 ele = driver.find_element_by_xpath('//*[@id="kw"...
text()方法:driver.find_element(By.XPATH,"//*[@class = 'lavalamp-item']/a[text()= '体育']") 这样就很简单了。 附上一段代码: # coding = utf-8from selenium import webdriverimport timefrom selenium.webdriver.common.by import Bydriver = webdriver.Chrome()url = 'http://news.baidu.com'...
find_elements_by_xpath find_elements_by_link_text find_elements_by_partial_link_text find_elements_by_tag_name find_elements_by_class_name find_elements_by_css_selector 1.3.1.通过ID查找元素 当你知道一个元素的 id 时,你可以使用本方法。在该策略下,页面中第一个该 id ...
Get Started free HomeGuideHow to use XPath in Selenium? (using Text, Attributes, Logical Operators) How to use XPath in Selenium? (using Text, Attributes, Logical Operators) Seleniumis a top choice for developers to automatecross browser testingof web applications. Selenium offers various choices...