selenium Is the most widely used open source Web UI( The user interface ) One of the automated test suites .
selenium: Automated test tool
Let my program connect to the browser , Let the browser complete all kinds of complex operations , We only accept the final result
You can open a browser . Then operate the browser like a person , Programmers can selenium Directly extract all kinds of information on the web page
1、
pip install selenium -i Tsinghua source
2、 Download browser driver ; https://npm.taobao.org/mirrors/chromedriver
3、 Unzip the browser driver chromedriver Put it in python The folder where the interpreter is located
4、 Give Way selenium Launch Google browser
---- First create the browser object ( With Chrome For example )
driver = Chrome()
Use Get Method -driver.get("www.yiibai.com")
;
Use Navigate Method -driver.navigate().to("https://yiibai.com/selenium/");
web.find_element_by_xpath('//[@id="JuserName"]').send_keys('***')
driver.find_element_by_xpath().clear();
driver.find_element_by_xpath(**).getText();
driver.find_element_by_xpath(**).click();
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
driver.close();
driver.quit();
driver.switch_to_window(web.window_handles[])
Return to previous window
web.switch_to_default_content()
web.switch_to_frame(**)
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
14. Implementation of headless browser
opt = Options()
```java
# opt.add_argument("--headless")
# opt.add_argument("--disable-gpu")
opt.add_argument('--disable-blink-features=AutomationControlled')
#1. Create a browser object
web = Chrome(options=opt)
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
opt = Options()
# opt.add_argument("--headless")
# opt.add_argument("--disable-gpu")
opt.add_argument('--disable-blink-features=AutomationControlled')
#1. Create a browser object
web = Chrome(options=opt)
# 2. Open a web address
web.get("https://kyfw.12306.cn/otn/resources/login.html")
print(web.title)
web.find_element_by_xpath('//*[@id="toolbar_Div"]/div[2]/div[2]/ul/li[2]/a').click()
web.find_element_by_xpath('//*[@id="J-userName"]').send_keys(' user name ')
web.find_element_by_xpath('//*[@id="J-password"]').send_keys(' password ')
web.find_element_by_xpath('//*[@id="J-login"]').click()
time.sleep(3)
index = web.find_element_by_xpath('//*[@id="nc_1__scale_text"]/span')
ActionChains(web).drag_and_drop_by_offset(index, 300, 0).perform()
from chaojiying import Chaojiying_Client
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
web = Chrome()
web.get("https://www.chaojiying.com/user/login/")
image = web.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[1]/form/div/img").screenshot_as_png
chaojiying = Chaojiying_Client(' user name ', ' password ', 'id')
print(chaojiying.PostPic(image, 1902))
dic = chaojiying.PostPic(image, 1902)
vs = dic['pic_str']
web.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input").send_keys(' user name ')
web.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input").send_keys(' password ')
web.find_element_by_xpath("/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input").send_keys(vs,Keys.ENTER)