SELENIUM browser automation tool. General consideration.
0. Selenium isn’t a browser it is just a driver to use other browsers.
1. How to install Selenium. // From virtual enviroment type $ pip install selenium
2. I need to install driver for Firefox. Download from github.com/mozilla/geckodriver/releases
3. Unzip driver and put it to $PATH directory. // $ mv geckodriver /usr/local/bin // Test it $ geckodriver
3.1 Install webdriver for Chromium // Doesn't work
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.f-controls.ru")
3.1 Download selenium-manager from github.com/SeleniumHQ/selenium/tree/trunk/common/manager/linux to auto manage drivers. // It is only 64-bit systems
4. To start browser from script:
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys # Use keyboard
o = Options()
b = Firefox(options=o)
b.get('https://duckduckgo.com')
b.set_window_size(1120, 550)
s = b.find_element(By.ID,'search_form_input_homepage')
s.send_keys('hello python')
s.submit() # Or s.send_keys(Keys.ENTER) or s.click()
5. To set headless mode o.headless=True
6. To close b.close() // quit()
7. To put content into BS4 library use content = b.page_source
8. Very useful feature is to execute JS // b.execute_script(“ type JS code here”)
9. To set directory for files download:
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "<download_dir>")
b.webdriver.Firefox(firefox_profile=profile)
10. Selenium has not sufficient functionality to download file.
11. To add start parameters use o.add_argument("--window-size=1120,800")
12. To open new tab use b.switch_to.new_window('tab') to switch back b.switch_to.window(‘[b.window_handles[0]’) like b.switch_to.window(‘15’)
13. To mouse click s.click()
13.1. Hover with mouse:
ActionChains(b)\
.move_to_element(ele)\
.click(ele)\
.perform()
14. Got error: Element is not reachable by keyboard.
14.1. Try to add inp.location_once_scrolled_into_view// No
14.2. Try to scroll to very top of window // b.execute_script("window.scrollTo(0,0) // No
14.3. Looks like webpage add some new class from time to time. But how they detect me? Try to change fonts in browser to spoof canvas.
14.4. What if I will get input element each time befor using? // No
14.5. Try to use By.CLASS_NAME instead of By.CSS_SELECTOR.
14.6. Probably I have to check if the element is displayed before use it. Otherwise use another element. // elem.is_displayed()
15. Selenium on remote machine.
15.1. I need Selenium Grid on remote machine to reduce time. It is kind of server.
15.2. On local machine:
webdriver.Remote(command_executor=server, options=options)
16. How to find element inside another element? Start XPATH with comma like .//div
16. Download Selenium Server Grid from selenium.dev/downloads
17. To start grid // java -jar selenium-server-<my_version>.jar standalone // Got nothing maybe I have to install browser drivers
17.1 Previously I have to install java with $ sudo apt install default-jre
17.2 Install selenium. $ pip install selenium from default enviroment and add to PATH (see above)
17.3 Download selenium-manager binaries and put it in the folder with server then $ java -jar selenium-server-<my_version>.jar standalone --selenium-manager true // Works only for 64-bit OS. It downloads all nesessary drivers.
17.4. I've got an error WARN [NodeOptions.addDetectedDrivers] - No drivers have been configured or have been found on PATH // But I have geckodriver installed
17.5. Try to install chrome driver from chromedriver.chromium.org/downloads // Nothing. Probably it is also only for 64 bit.
17.6. Test on browser
1. Just type Enter:
ActionChains(b)\
... .key_down(Keys.ENTER)\
... .perform()
2. Use better inp.send_keys("solution \n")
3. How to select parent element? // ref = b.find_element( By.XPATH, "//a[contains(text(),'Content')]/ancestor::li" )
4. How to get html of selenium element. // ref.get_attribute('innerHTML') or ref.get_attribute('outerHTML')
5. How to get iframe? // ifr = b.find_element(By.CSS_SELECTOR, ".modal .modal-body iframe") then b.switch_to.frame(ifr)
6. How to select element by unusual attribute like href. // b.find_element(By.XPATH,"//a[@href='url']")
7. How to move pointer. // ...import ActionChains
ActionChains(b)\
.move_to_element(ele)\
.perform()
8. How to fid an element by class and id and tag? Do without spaces b.find_element(By.CSS_SELECTOR, "#modal.iframe")