I have downloaded the latest python version(3.8.5) and google chrome version(84) and corresponding chromedriver version(84). But the browser does not respond when running the program.
from time import sleep
from selenium import webdriver
class Bot:
def __int__(self):
self.driver = webdriver.Chrome("C:\\Windows\\chromedriver.exe")
self.driver.get("www.google.com")
my_bot = Bot()
def main():
my_bot()
if __name__ == 'main':
main()
The above is my python code
from time import sleep
from selenium import webdriver
class Bot:
def __init__(self, url):
# path to chrome driver in your local machine
driver_path = "C:\\Windows\\chromedriver.exe"
# Loading the chrome driver
self.driver = webdriver.Chrome(driver_path)
# Using the above-loaded WebDriver to open the URL in the chrome
# browser
self.driver.get(url)
def main():
# Creating a object of Bot class with URL as an input parameter
# Don't forget to use https:// else your URL won't get open
my_bot = Bot(url="https://www.google.com")
# A sleep timer of 10 sec to let webdriver load the given URL and `
# display it to you for 10 sec
sleep(10)
# Closing the bot object which we created
# After 10 sec the bot object will be closed
my_bot.close()
if __name__ == '__main__':
# Calling the main function where the Bot object is created
main()
Its rather more easy in three lines!!
from selenium import webdriver
driver = webdriver.Chrome("G:\\Downloads\\chromedriver_win32\\chromedriver.exe")
driver.get('https://www.google.com/')
And you are done!
Related
I'm trying to use Selenium and Beautiful Soup together with Python but having a rather strange issue - Selenium window stays opened only if requests module is not imported, otherwise it stays opened for like 1 second and closes.Important thing is that this only happens when I create the class in another file - when I create the class in the same file it stays opened normally.
Below are the two versions of code - 1st one where window stays opened, 2nd one where window instantly closes:
1: WORKS
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from test import Watcher
service = Service("C:\Program Files (x86)\Development\chromedriver.exe")
driver = webdriver.Chrome(service=service)
tw = Watcher(driver)
tw.open_browser()
# OTHER FILE CALLED test
class Watcher:
def __init__(self, driver):
self.driver = driver
def open_browser(self):
self.driver.get("https://www.google.com")
2: CLOSES
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from test import Watcher
import requests
service = Service("C:\Program Files (x86)\Development\chromedriver.exe")
driver = webdriver.Chrome(service=service)
tw = Watcher(driver)
tw.open_browser()
# OTHER FILE CALLED test
class Watcher:
def __init__(self, driver):
self.driver = driver
def open_browser(self):
self.driver.get("https://www.google.com")
I'm wondering if this has something to do with the Keep-Alive and Connection Pooling in urllib3, even though the requests module is not used in the program code.
When your program terminates, the HTTP connections are released and the browser window closes.
Add this and you'll see that the browser window stays open until the program has finished running. The program simply terminates perfectly normally with no errors.
from time import sleep
for _ in range(10):
sleep(1)
[Edit] I have a solution!
detach (Boolean) — whether browser is closed when the driver is sent the quit command.
See Class: Selenium::WebDriver::Chrome::Options
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from watcher import Watcher
import requests
service = Service("C:\Program Files (x86)\Development\chromedriver.exe")
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=service, options=options)
tw = Watcher(driver)
tw.open_browser()
Good evening,
I am currently working on a selenium Bot, that Posts some of my articles on the Platform Ebay-Kleinanzeigen, but somehow I cannot access the Login page with my Bot.
from selenium import webdriver
from time import sleep
class Ebaybot:
def __init__(self, username, pw):
self.driver = webdriver.Edge(executable_path=r'C:\Users\dsbh0\Desktop\Ebaybot\msedgedriver.exe')
self.driver.get("https://www.ebay-kleinanzeigen.de")
sleep(2)
self.driver.find_element_by_xpath("/html/body/div[2]/div/div/div/div/div[3]/button[1]").click()
sleep(5)
self.driver.find_element_by_xpath("/html/body/header/section[1]/section/nav/ul/li[3]/a")\
.click()
sleep(5)
# self.driver.find_element_by_xpath("/html/body/div[1]/div[2]/div/div[1]/form/div[1]/div/div/input")\
# .click()
# sleep(2)
# login_field = self.driver.find_element_by_xpath("/html/body/div[1]/div[2]/div/div[1]/form/div[1]/div/div/input")\
# .send_keys(username)
# self.driver.find_element_by_xpath("/html/body/div[1]/div[2]/div/div[1]/form/div[2]/div/div/input")\
# .click()
# pw_field = self.driver.find_element_by_xpath("/html/body/div[1]/div[2]/div/div[1]/form/div[2]/div/div/input")\
# .send_keys(pw)
my_bot = Ebaybot("User","pw")
I already tried to use Chromedrive and I've had the same issue with it.
This is how it looks like
When I run the code Chrome opens the URL but after about 2 seconds it crashes. It also says on the top of the chrome window "Chrome is being controlled by automated test software"
I am running the compatible version of the chrome driver for my version of chrome.
This is my code. How can I fix the crashing?
#from config import keys
from selenium import webdriver
def order():
driver = webdriver.Chrome('./chromedriver')
driver.get('https://www.youtube.com/')
if __name__ == '__main__':
order()
as you created the driver object in the scope of order()
after the execution of the order() is done all the local variables are removed.
You must have to declare driver as the global variable
from selenium import webdriver
# declare global varible driver
driver = None
def order():
driver = webdriver.Chrome('./chromedriver')
driver.get('https://www.youtube.com/')
if __name__ == '__main__':
order()
Otherwise, you can add time.sleep() to wait for a while
import time
from selenium import webdriver
def order():
driver = webdriver.Chrome('./chromedriver')
driver.get('https://www.youtube.com/')
# will wait for 5 seconds
time.sleep(5)
if __name__ == '__main__':
order()
I am using Selenium and Chrome webdriver in Python, when I'm running script it opens chrome normally, but it waits in "data;" page around 10 seconds and than go to page normally. Can i skip that waiting time and go to page instantly?
There is my code:
class Instance():
driver=None
url=None
def __init__(self, url):
self.url=url
def runInstance(self):
self.driver = webdriver.Chrome(executable_path = "C:\chromedriver.exe")
self.driver.get(self.url)
instance = Instance("https://www.youtube.com/")
instance.runInstance()
Windows 10 x64 / Python 2.7 / Selenium
I am trying to build a tool to scrape my ticket queue for unassigned tickets, open them and look for keywords, then do other things. But for now, I can't seem to figure out getting the code to open more than the first URL. I keep getting a StaleElementReferenceException error; and I do not understand why.
I'm building on top of examples I have found. This may not even be a good way to go about this. I am open to a new direction as well.
The goal for this will be to have something scrape the queue every X and when certain keywords are found offer me a prompt to assign it. It needs to run on its own while I am doing other tasks so it can not interfere with my keystrokes.
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
import unittest
class LoginTest (unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("https://TICKETS.COMPANY.COM/TEAM/MINE")
def test_Login(self):
driver = self.driver
table = "UNASSIGNED-TICKETS"
# Select the UNASSIGNED-TICKETS tab
selectUnassignedTab = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_link_text('Unassigned'))
selectUnassignedTab.click()
# Grab all of the Ticket URLs
unlockedTickets = WebDriverWait(driver, 10).until(lambda driver: driver.find_elements_by_xpath("//table[#id='unlocked-tickets']/tbody/tr[#role='row']/td[#class='nowrap']/a[#href]"))
counter = 1
dictURLs={}
for ticket in unlockedTickets:
ticketUrl = ticket.get_attribute('href')
# Troubleshooting: Make sure URLs are grabbed.
print ticket.get_attribute('href')
# Stuff them in a dict
dictURLs["string{0}".format(counter)]=ticketUrl
# Open each ticket (NOT WORKING)
# driver.get(ticketUrl) <--- Causes the Stale Element Error
if counter == 1:
# Wait for the User and Pass fields to load. Then assign them.
emailFieldElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_name('username'))
passFieldElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_name('password'))
# Log in information.
emailFieldElement.clear()
emailFieldElement.send_keys("USERNAME-HERE")
passFieldElement.clear()
passFieldElement.send_keys("PASSWORD-HERE")
passFieldElement.submit()
counter = counter + 1
def tearDown(self):
sleep(15)
self.driver.quit()
if __name__ == '__main__':
unittest.main()
I used this code to open multiple windows and it worked like a charm
from selenium import webdriver
import threading
import time
def test_logic():
driver = webdriver.Firefox()
url = 'https://www.google.co.in'
driver.get(url)
# Implement your test logic
time.sleep(2)
driver.quit()
N = 5 # Number of browsers to spawn
thread_list = list()
# Start test
for i in range(N):
t = threading.Thread(name='Test {}'.format(i), target=test_logic)
t.start()
time.sleep(1)
print t.name + ' started!'
thread_list.append(t)
# Wait for all thre<ads to complete
for thread in thread_list:
thread.join()
print 'Test completed!'