from selenium import webdriver
browser = webdriver.Chrome("chromedriver.exe")
url = 'https://www.baidu.com/'
browser.get(url)
content = browser.page_source
print(content)
页面信息
print(browser.title)
print(browser.current_url)
browser.save_screenshot("baidu.png")
print(browser.get_cookies())
元素信息
button = browser.find_element(By.ID, "su")
print(button.get_attribute('class'))
print(button.tag_name)
a = browser.find_element(By.LINK_TEXT, "直播")
print(a.text)
元素定位
from selenium import webdriver
from selenium.webdriver.common.by import By
button = browser.find_element_by_id("su")
print(button)
button = browser.find_element_by_name("wd")
print(button)
button = browser.find_element_by_xpath("//input[@id='su']")
print(button)
button = browser.find_elements_by_xpath("//input[@id='su']")
print(button)
button = browser.find_element_by_tag_name("input")
print(button)
button = browser.find_element_by_css_selector("#su")
print(button)
button = browser.find_element_by_link_text('直播')
print(button)
定位方式 By
id By.ID
name By.NAME
class_name By.CLASS_NAME
tag_name By.TAG_NAME
link_text By.LINK_TEXT
partial_link_text By.PARTIAL_LINK_TEXT
css_selector By.CSS_SELECTOR
xpath By.XPA
|