Skip to content

Instantly share code, notes, and snippets.

@scottie
Last active September 19, 2023 16:53
Show Gist options
  • Save scottie/7411c96b3918d8072db814ddfe05c474 to your computer and use it in GitHub Desktop.
Save scottie/7411c96b3918d8072db814ddfe05c474 to your computer and use it in GitHub Desktop.
selenium-testing-with-pycharm

Software versions used:

Selenium 4.12.0 (pip3 show selenium)
Ubuntu 23.04 (lsb_release -r)
Python3 3.11.4 (python3 --version)
Pip23.0.1 (pip3 --version)
Pytest 7.4.2 (pip3 show pytest)
Firefox 116.0.3 (firefox --version)
Snap 2.60.3 (snap version)
Kernel 6.2.0-32-generic

Install Firefox

use below if you can:

sudo apt update && sudo apt upgrade
sudo apt install firefox

Otherwise use below, the first comes with the driver support out of the box the second requires you to see caveats section:

sudo apt update && sudo apt upgrade
sudo apt install snapd
sudo systemctl start && snapd sudo systemctl enable snapd
snap version
sudo snap install firefox

...See Caveats

OR .... Install Chromium

sudo apt update
sudo apt install chromium-browser
sudo apt install chromium-chromedriver

Install Python

  • Ubuntu 22+ comes with python3
  • Python3 comes installed with pip

Otherwise:

sudo apt-get install python3

Make Project Folder / CD to folder

mkdir projectName
cd projectName

Create Virtual Environment

python3 -m venv projectName

Use Environment

source projectName/bin/activate

Simple type deactivate to deactivate

Install Selenium & Pytest

pip3 install selenium pytest

Create Test

You can use the Selenium IDE browser add-on to record tests then export and fix like below:

import  pytest
import  time
import  json
from  selenium  import  webdriver
from  selenium.webdriver.firefox.options  import  Options
from  selenium.webdriver.firefox.service  import  Service
from  selenium.webdriver.common.by  import  By
from  selenium.webdriver.common.action_chains  import  ActionChains
from  selenium.webdriver.support  import  expected_conditions
from  selenium.webdriver.support.wait  import  WebDriverWait
from  selenium.webdriver.common.keys  import  Keys
from  selenium.webdriver.common.desired_capabilities  import  DesiredCapabilities

options  =  Options()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
#options.log.level = "trace"
#options.add_argument('-v')
#options.add_argument("-headless")

service  =  Service(executable_path="/snap/bin/geckodriver") # specify the path to your geckodriver
driver  =  webdriver.Firefox(options=options, service=service)
driver.set_window_size(1920, 1080)
driver.maximize_window()
driver.implicitly_wait(10)

print("[!] Using browser version:"  +  driver.capabilities["browserVersion"])
driver.get("https://www..com/")
driver.implicitly_wait(20)
print("[*] Doing Something !")
driver.find_element(By.NAME, "query").click()
# 4 | type | name=query | flate ear
driver.find_element(By.NAME, "query").send_keys("flate ear")
# 5 | sendKeys | name=query | ${KEY_DOWN}
driver.find_element(By.NAME, "query").send_keys(Keys.DOWN)
# 6 | type | name=query | flate earth
driver.find_element(By.NAME, "query").send_keys("flate earth")
# 7 | click | css=.search-submit > .svg-inline--fa |
driver.find_element(By.CSS_SELECTOR, ".search-submit > .svg-inline--fa").click()
# 8 | sendKeys | name=query | ${KEY_ENTER}
driver.find_element(By.NAME, "query").send_keys(Keys.ENTER)
# 9 | runScript | window.scrollTo(0,0) |
driver.execute_script("window.scrollTo(0,0)")
# 10 | waitForElementPresent | linkText=QE 'LIVE': #1 Globe-Killer ('Collectors Edition') | 30000
WebDriverWait(driver, 30).until(expected_conditions.presence_of_element_located((By.LINK_TEXT, "QE \'LIVE\': #1 Globe-Killer (\'Collectors Edition\')")))
# 11 | runScript | window.scrollTo(0,0) |
driver.execute_script("window.scrollTo(0,0)")
# 12 | assertElementPresent | linkText=QE 'LIVE': #1 Globe-Killer ('Collectors Edition') |
elements  =  driver.find_elements(By.LINK_TEXT, "QE \'LIVE\': #1 Globe-Killer (\'Collectors Edition\')")
assert  len(elements) >  0
print("[-] Done !")

Run

From inside the directory your test_file.py is, run:

pytest

if you get a error liek the site does not attempt to load please fix with this:

http_proxy= pytest

Stackoverflow Solution

Caveats

Because firefox un the latest ubuntu is now installed with snap some code changes must be made to find the firefox web driver. One must set the executable_path to /snap/bin/geckodriver

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service

def open_firefox():
   """Open Firefox and navigate to google.com."""
  options = Options()
  options.add_argument('--no-sandbox')
  options.add_argument('--disable-dev-shm-usage')
  #options.log.level = "trace"
  #options.add_argument('-v')
  #options.add_argument("-headless")

  service = Service(executable_path="/snap/bin/geckodriver") # specify the path to your geckodriver
  driver = webdriver.Firefox(options=options, service=service)
  driver.set_window_size(1920, 1080)
  driver.maximize_window()
  driver.implicitly_wait(10)

  driver.get("https://www.google.com") # navigate to Google
  
if __name__ == "__main__":
    open_firefox()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment