Getting an error using selenium with python

96 Views Asked by At

When I use selenium in python to open a website in chrome it give me an Attribute Error This is my code :

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

browser = webdriver.Chrome(ChromeDriverManager().install())
browser.get('https://google.com')

And it give me this error : AttributeError: 'str' object has no attribute 'capabilities'

I am expecting to open the chrome in google. And I tried to uninstall the modules and reinstall the modules one more time and tried to download the driver manually put in the option attribute but didn't work.

1

There are 1 best solutions below

0
5rod On

You should use playwright instead, as it is a much newer python library and has many advantages over selenium.

Installation: pip install playwright, followed by: playwright install, and webkit, firefox, and chromium will be downloaded. Don't worry, you can still use chrome and msedge. Snippet of code:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(channel="chrome") #launches chrome browser
    page = browser.new_page()
    page.goto('https://example.com')
    print(page.title())
    page.locator('path to element').fill('text')
    browser.close()

Since you're using chrome, you want to add the channel argument like I have, but keep the chromium part. (Just copy my code and it should work). I showed as an example just a few of the functions the playwright library has. Note that playwright launches in headless mode by default, so you'll need to add an argument if you want to be able to see the browser launching.

The reason I suggest you use playwright instead of selenium is that playwright does not require a webdriver, and it is a much newer and up to date python library, and it has many more applications and features. This will also solve your AttributeError.