--- tags: Python --- # Selenium studying note ###### tags: `selenium` `python` ## platform system: lenovo legion y530-15ich OS: ubuntu 20.04.3 LTS, kernel 5.11.0-27 python3 version: 3.8.10 selenium version: 3.141.0 web browser driver: 0.29.1 (geckodriver) (for firefox) ## environment installing / setting method (virtualenv is optional) ### install selenium ```bash= # create a new virtualenv environment virtualenv -p python3 selenium_virtualenv cd ./selenium_virtualenv . ./bin/activate # install selenium with pip pip3 install selenium ``` ### download geckodriver and copy to /usr/bin we need to download web browser driver for web browser I use firefox, so I use geckodriver url: https://github.com/mozilla/geckodriver/releases and copy geckodriver file to /usr/bin ```bash= tar zxvf ./geckodriver-v0.29.1-linux64.tar.gz cp ./geckodriver /usr/bin ``` ### sample code 1. open webbrowser => go to the website via URL => get title? => close browser ```python= from selenium import webdriver browser = webdriver.Firefox() # open browser browser.get('https://www.linuxhint.com') # go to the website print('Title: %s' % browser.title) # output: Linux Hint tmp = input('press enter to continue: ') browser.quit() ``` 2. open webbrowser => go to the website via URL => get content in div id: lipsum => print content in div id: lipsum => close ```python= from selenium import webdriver from selenium.webdriver.firefox.options import Options # ? from selenium.webdriver.common.keys import Keys # ? firefoxOptions = Options() # ? firefoxOptions.add_argument("-headless") # ? browser = webdriver.Firefox() # open browser browser.get('https://www.lipsum.com/feed/html') # go to the website lipsum = browser.find_element_by_id('lipsum') # get the content which is in div id: lipsum print(lipsum.text) # print text in div id: lipsum # result will be the string in the blue part of picture showed below tmp = input('press enter to continue: ') browser.quit() # close browser ``` ※ result of line.9 is content of blue rectangle in the picture below ![](https://lh3.googleusercontent.com/d/1W8Xur8Q_35y2edUmqNo9TpCxLspXh6hy) 3. open webbrowser => go to the website via URL => get content in li of ol id: nameList of div id: results => print content in in li of ol id: nameList of div id: results => close ```python= from selenium import webdriver from selenium.webdriver.firefox.options import Options # ? from selenium.webdriver.common.keys import Keys # ? firefoxOptions = Options() # ? firefoxOptions.add_argument("-headless") # ? browser = webdriver.Firefox() # open browser browser.get("http://random-name-generator.info/") # go to the website nameList = browser.find_elements_by_css_selector('div.results ol.nameList li') # get the content which are in li of ol.nameList of div id: result for name in nameList: print(name.text)# print text in li of ol.nameList of div id: result # result will be the string in the blue part of picture showed below tmp = input('press enter to continue: ') browser.quit() ``` ※ result of line.10 is content of blue rectangle in the picture below ![](https://lh3.googleusercontent.com/d/1gNJF4H_erXd04tu8uiTUIir5yy88dX2k) ※ about difference with .find_element_by_id, .find_elements_by_css_selector and others can refer to: https://selenium-python.readthedocs.io/locating-elements.html, Ch.4 Locating Elements 4. open webbrowser => go to the website via URL => fill in value "selenium" into input id: kw => click the button of input id: su => print current url => close ```python= from selenium import webdriver import time browser = webdriver.Firefox() browser.get("http://www.baidu.com") browser.find_element_by_id('kw').send_keys('selenium') # fill in value "selenium" in blank which is in input id: kw tmp = input('press enter to continue: ') browser.find_element_by_id('su').click() # click button which is in input id: su time.sleep(3) print(browser.current_url) # print current_url tmp = input('press enter to continue: ') browser.quit() ``` ![](https://lh3.googleusercontent.com/d/1jtwk0K-pqZQ5cZeSN0Gi_MedNwNcvxT7) ![](https://lh3.googleusercontent.com/d/1rlMK7k3NdAUE4u9cq5C1F5xz71Dckw23) ![](https://lh3.googleusercontent.com/d/1Z75cF5vqvTmbKihcz8-sCgGxUrtwQDHJ) ※ about .send_keys, .click and others can refer to: https://selenium-python.readthedocs.io/locating-elements.html, Ch.7 WebDriver API ## to do list How about corporate selenium with pykeyboard? ## tmp_source https://www.google.com/search?channel=fs&client=ubuntu&q=selenium%E6%95%99%E5%AD%B8 https://medium.com/marketingdatascience/selenium%E6%95%99%E5%AD%B8-%E4%B8%80-%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8webdriver-send-keys-988816ce9bed https://medium.com/marketingdatascience/%E5%8B%95%E6%85%8B%E7%B6%B2%E9%A0%81%E7%88%AC%E8%9F%B2%E7%AC%AC%E4%BA%8C%E9%81%93%E9%8E%96-selenium%E6%95%99%E5%AD%B8-%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8find-element-s-%E5%8F%96%E5%BE%97%E7%B6%B2%E9%A0%81%E5%85%83%E7%B4%A0-%E9%99%84python-%E7%A8%8B%E5%BC%8F%E7%A2%BC-b66920fc8cab https://medium.com/marketingdatascience/%E8%B7%9F%E8%91%97ig%E6%BD%AE%E6%B5%81%E4%BE%86%E7%88%AC%E8%9F%B2-%E5%A6%82%E4%BD%95%E7%8D%B2%E5%8F%96%E8%B2%BC%E6%96%87%E5%9C%96%E7%89%87-%E5%BD%B1%E7%89%87%E9%80%A3%E7%B5%90-%E7%B3%BB%E5%88%974-%E9%99%84python%E7%A8%8B%E5%BC%8F%E7%A2%BC-fe9f3f3e3f62 https://www.learncodewithmike.com/2020/05/python-selenium-scraper.html https://www.google.com/search?channel=fs&client=ubuntu&q=linux+selenium+firefox https://linuxhint.com/using_selenium_firefox_driver/ https://www.jianshu.com/p/dc95038d9292 (important) https://selenium-python.readthedocs.io/ ## source https://selenium-python.readthedocs.io/ https://stackoverflow.com/questions/39179320/how-to-install-firefoxdriver-webdriver-for-python3-selenium-on-ubuntu-16-04 https://stackoverflow.com/questions/46809135/webdriver-exceptionprocess-unexpectedly-closed-with-status-1 https://linuxhint.com/using_selenium_firefox_driver/ https://www.jianshu.com/p/dc95038d9292