selenium

 

 

xpath 按文本内容进行搜索

driver.find_element_by_id('//span[normalize-space(text())="基本信息"]')

 

 

 元素定位测试:

> $x('xpath')

 

 

 

文本框输入停顿:

def sendKeys(textBox, str):
for _char in str:
time.sleep(0.05)
textBox.send_keys(_char)

 

 

example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome("D:\\SoftWare\\chromedriver.exe")
driver.get("https://www.baidu.com/")
WebDriverWait(driver, 10).until(EC.title_is(u"百度一下,你就知道"))
driver.maximize_window()

driver.find_element(By.ID, 'kw').send_keys("selenium automation" + Keys.ENTER)
first_result=WebDriverWait(driver, 5).until(presence_of_element_located((By.XPATH, '//*[@id="content_left"]/div/h3[1]')))
print(first_result.get_attribute("textContent"))

 

 

 

参考资料

https://www.selenium.dev/documentation/en/

https://selenium-python.readthedocs.io/

https://www.runoob.com/xpath/xpath-syntax.html

posted @ 2021-02-18 12:03  sam_wang10  阅读(44)  评论(0编辑  收藏  举报