Selenium 4 / Python

Hi there,

Yesterday, after more than one year without working in one of my pet projects, I cloned the repository in a new Ubuntu desktop 20.04

Most of the GUI code worked out, but it wasn’t the case for the Selenium bits. So I decided to take a closer look, and then, I realized that Selenium python bindings had been updated from the stable version 3.141.0 to the new pre-release 4.0.0a5

After many tries, I was only getting these messages:

DeprecationWarning: executable_path has been deprecated, please pass in a Service object

DeprecationWarning: firefox_profile has been deprecated, please pass in a Service object

So, I took a look to the source code and, finally, I was able to solve the issue:

#!/usr/bin/env python3 # -*- coding: utf-8 -*- 
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

# Options options = Options()

## Option for using a custom Firefox profile options.profile = '/home/t00m/.basico/opt/webdrivers/basico.default'

## Enable headless options.headless = False

# Specify custom geckodriver path service = Service('/home/t00m/.basico/opt/webdrivers/geckodriver')

# Test browser = webdriver.Firefox(options=options, service=service)
browser.get('https://dev.to')
print(browser.title)
browser.quit()

Bonus track

My application needs to load a page via SSO. In the past, I used a timeout. Now, I keep the timeout but I’ve added the following code to skip it if it loads the page faster:

# Extra imports from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# Test timeout = 10
browser = webdriver.Firefox(options=options, service=service)
browser.get('https://dev.to')

try:
    element_present = EC.presence_of_element_located((By.ID, 'snack-zone'))
    WebDriverWait(browser, timeout).until(element_present)
except TimeoutException:
    print("Timed out waiting for page to load")
finally:
    print("Page loaded")
print(browser.title)
browser.quit()

Take care. Stay safe.

原文链接:Selenium 4 / Python

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
Fight for the things you love no matter what you may face, it will be worth it.
不管你面对的是什么,为你所爱的而奋斗都会是值得的
评论 抢沙发

请登录后发表评论

    暂无评论内容