How to open a browser with Python, Selenium and geckodriver?

Asked

Viewed 157 times

2

I wrote an automation to follow people on instagram, but when I put Run in the code, it returns me in the log with no error, and still doesn’t open the browser for me to test the code. I think there’s something wrong at the beginning of my code that’s not allowing me to open the browser but I don’t know what it might be. This is the code I’m using to open Firefox:

from selenium.webdriver.common.keys import Keys
import time


class InstagramBot:
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.driver = webdriver.Firefox(executable_path=r'C:\geckodriver\geckodriver.exe')```

1 answer

1

Probably what is missing and insert in the code the url of the site.

And right after iserir the ex option.: web.get(url)

follows an example of code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located

with webdriver.Firefox() as driver:
    wait = WebDriverWait(driver, 10)
    driver.get("https://google.com/ncr")
    driver.find_element(By.NAME, "q").send_keys("cheese" + Keys.RETURN)
    first_result = wait.until(presence_of_element_located((By.CSS_SELECTOR, "h3>div")))
    print(first_result.get_attribute("textContent"))

source: https://www.selenium.dev/documentation/en/

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.