Selenium Usage Summary (Java version)

Selenium is widely used in recent development because it has not been used before and has gone too far. This is a record.

Key points of configuration: 1 The web driver should correspond to the browser version. Chrome uses the chrome driver and Chrome browser, and Firefox uses geckodrive and Firefox browser 2 Headless support: MAC environment is used for local development, which is supported by default; Linux needs to be built with xvf8 (virtual GUI) 3.maven project. Selenium-3.9.1 or the latest version 4.linux configuration reference: Chrome:; Firefox:

Chrome startup parameter reference: 1 System environment variable configuration: webdriver chrome. driver=DRIVER_ PATH 2. Common options configuration: 3 Webdriver instantiation: system setProperty("webdriver.chrome.driver",env.getProperty("path.chrome.driver" WebDriver webDriver = ChromeOptions options = options.addArguments("--headless"); options. addExtensions( File(env.getProperty("path.chrome.proxy"))); webDriver = ChromeDriver(options); } } (webDriver != }

1. System environment variable configuration: webdriver gecko. driver=DRIVER_ PATH 2. Common options configuration: preference configuration: 3 Webdriver instantiation: system setProperty("webdriver.gecko.driver",env.getProperty("path.gecko.driver" WebDriver webDriver = FirefoxOptions options = options.addArguments("--headless"); FirefoxProfile profile = profile. addExtensions( File(env.getProperty("path.chrome.proxy"))); profile. setPreference("permissions.default.image",2); Webdriver =}} (webdriver! =} 4. Note: the default loading will mask some requests (JS requests, etc.). Reference: 1. Get page: driver.get (URL); 2. Close the page: driver close(); Close process: driver quit(); 1. The thread is forced to sleep and wait, thread sleep(3000); 2. Implicit waiting (Global waiting), set the waiting time for all operations and throw exceptions when timeout; 3. You can set explicit waiting (controllable waiting, recommended), waiting trigger event and waiting time for a single operation. You can customize events and throw exceptions over time. Webdriverwait wait = webdriverwait (webdriver, 60); wait. until(ExpectedConditions.presenceOfElementLocated(By.id("xx"))); WebElement frame = wait. until((ExpectedCondition ) driver -> = driver. findElement(By.id("iframepage" String page = driver.getPageSource(); String url = driver. getCurrentUrl(); WebElement element = driver. findElement(By.id("xx")); element. sendKeys("test"); element. click(); element. sendKeys(Keys.BACK_SPACE); 1. Window operation string handle = driver getWindowHandle(); Set handles = driver. getWindowHandles(); (! driver.switchTo(). window(h); } } 2. Form operation and frame switching need to be obtained from outside to inside layer by layer, which can be processed according to pagesource 3 Pop up operation

String proxyServer = "1.2.3.4:666" Proxy proxy = options. setProxy(proxy);
Use browsermobproxy as proxy (or other proxy) browsermobproxyserver BMP server = BMP server.setchainedproxy (inetsocketaddress ("proxy. Com", 222)); bmpServer. chainedProxyAuthorization("user","pwd",AuthType.BASIC); bmpServer. setTrustAllServers(); bmpServer. start(11112); String proxyServer = "localhost:11112" Proxy proxy = options. setProxy(proxy); Local agents can be deployed separately in a distributed manner, with multiple nodes and ZK management
1. Chrome extension: the loading extension cannot be used in headless mode, and chrome options has not been resolved addExtensions( File(env.getProperty("path.chrome.proxy"))); Although chromium is executed by multiple processes, the JS engine is a single thread. When multiple windows are opened at the same time, only one page will be loaded. The next page will not be loaded until the loading is completed or the next window is opened, Reference (Firefox can load pages of multiple windows at the same time, and some requests will be shielded by default. 1. Through proxy settings, browsermobproxy server = server. Blacklistrequests ("http: / /. * \ \. Blacklist. COM /. *", 200); server. setBlacklist(); 2. Through the expansion setting, it is not fully integrated for the time being 1 Use grid to start multiple nodes in the correct connection mode. Note: when a single node uses multithreading, it is best to use geckodriver. The JS engine of chromium is executed by single thread
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>