Download the corresponding version of webdriver and Chrome browser
Download the corresponding version of webdriver and Chrome browser
http://chromedriver.storage.googleapis.com/index.html
1. Find the element
https://blog.csdn.net/qq_32897143/article/details/80383502
2. Explicit wait and implicit wait
Display waiting: execute when the element is found within a period of time, and no error is reported.
Implicit waiting: similar to time.sleep(10), it waits for a period of time before starting to find elements.
3. Close (close and quit)
close: Close the current page.
quit: Close the browser.
4. The presence of an embedded frame makes it impossible to locate the element
# 切换内嵌frame
fr=browser.find_element_by_xpath("//iframe[contains(@src,'https://uac.10010.com/portal/homeLoginNew')]")
browser.switch_to.frame(fr)
5. chrom browser is recognized
from selenium import webdriver
def getDriver():
options = webdriver.ChromeOptions()
options.add_argument("--disable-extensions")
options.add_argument("--disable-gpu")
#options.add_argument("--no-sandbox") # linux only
#options.add_argument('--proxy-server=127.0.0.1:8080') # 设置代理,目标url打不开
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(executable_path='C:\my_files\chromedriver.exe', options=options)
driver.execute_cdp_cmd("Network.enable", {
})
driver.execute_cdp_cmd("Network.setExtraHTTPHeaders", {
"headers": {
"User-Agent": "browserClientA"}})
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
return driver
driver = getDriver()
driver.maximize_window()
driver.get('https://www.baidu.com')#百度换成目标url
6.firefox settings headless and proxy
profile=webdriver.FirefoxOptions()
profile.add_argument('-headless') #设置无头模式
#设置代理服务器
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.http',IP)#IP为你的代理服务器地址:如‘127.0.0.0’,字符串类型
profile.set_preference('network.proxy.http_port', PORT) #PORT为代理服务器端口号:如,9999,整数类型
driver=webdriver.Firefox(options=profile)