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
Related
The Situation:
Firstly I would like to say that am new to selenium and decided to pick it up to practice some python. I am currently following a tutorial online and decided to make a youtube bot.
The Code:
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
import time
import random
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def login_with_username_and_password(browser, username, password):
# FILL UP THE LOGIN FORM
email_input = browser.find_elements('input[type=email]')
email = username
for letter in email:
email_input.send_keys(letter)
wait_time = random.randint(0,1000)/1000
time.sleep(wait_time)
next_button = browser.find_elements_by_css_selector("button")
time.sleep(2)
next_button[2].click()
time.sleep(2)
password_input = browser.find_element_by_css_selector('input[type=password]')
password = password
for letter in password:
password_input.send_keys(letter)
wait_time = random.randint(0,1000)/1000
time.sleep(wait_time)
next_button = browser.find_elements_by_css_selector("button")
time.sleep(2)
next_button[1].click()
confirm_button = browser.find_elements_by_css_selector("div[role=button]")
time.sleep(2)
if(len(confirm_button)>0):
confirm_button[1].click()
def click_on_agree_and_signin(browser):
# agree_button= browser.find_element_by_css_selector('button')
# time.sleep(2)
# agree_button.click()
signin_buttons= browser.find_elements_by_css_selector(".signin")
time.sleep(6) # Wait longer so the message pops up
while(len(signin_buttons)== 0):
signin_buttons= browser.find_elements_by_css_selector(".signin")
time.sleep(1)
signin_buttons[0].click()
def enter_search_term(browser,search_term):
# Enter text on the search term
search_input = browser.find_element_by_id("search")
for letter in search_term:
search_input.send_keys(letter)
wait_time = random.randint(0,1000)/1000
time.sleep(wait_time)
search_input.send_keys(Keys.ENTER)
def enter_comment(browser, comment):
comment_input = browser.find_element_by_css_selector("ytd-comment-simplebox-renderer")
entering_comment_actions = ActionChains(browser)
entering_comment_actions.move_to_element(comment_input)
entering_comment_actions.click()
for letter in comment:
entering_comment_actions.send_keys(letter)
wait_time = random.randint(0,1000)/1000
entering_comment_actions.pause(wait_time)
entering_comment_actions.perform()
time.sleep(1)
send_comment_button = browser.find_element_by_id("submit-button")
send_comment_button.click()
###########################################
# BOT STARTS HERE #
###########################################
driver=webdriver.Chrome()
driver.maximize_window
driver.get("https://www.youtube.com/")
all_search_terms = ['online marketing']
# Click Agree and Sing In
click_on_agree_and_signin(driver)
# Sign In
login_with_username_and_password(driver, "hey289895#gmail.com", "-1qa2ws3ed4rf-")
for search_term in all_search_terms:
enter_search_term(driver, search_term)
time.sleep(2)
thumbnails = driver.find_element_by_css_selector("ytd-video-renderer")
for index in range(1, 6):
thumbnails[index].click()
time.sleep(6)
enter_comment(driver, "love it")
driver.execute_script("window.history.go(-1)")
thumbnails = driver.find_element_by_css_selector("ytd-video-renderer")
time.sleep(1)
driver.close()
The Problem:
When Running this code it produces an error related to the find_element_by_css_selector method. Most frequently during the sign in phase as shown here.
The Question:
Can anybody explain what is going on here and where I am going wrong, as well as how I can fix this please.
First: it is only warning, not error.
In my version 3.141.0 I can use both methods
from selenium import webdriver
driver = webdriver.Chrome() # Firefox()
driver.find_elements_by_css_selector(...)
driver.find_elements_by_xpath(...)
# etc.
and
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome() # Firefox()
driver.find_elements(By.CSS_SELECTOR, ...)
driver.find_elements(By.XPATH, ...)
# etc.
but it seems they plan to remove functions find_elements_by_... in the future (in versions 4.x) and now find_elements_by_... still works but it shows warning that you should use second method find_elements(By.CSS_SELECTOR, ...).
You could use module warnings to hide these warnings but better start using only second method.
BTW:
In source code for find_elements_by_css_selector you can see it runs warning.warn(...) and next it runs find_elements(By.CSS_SELECTOR, ...)
To check what version you use
import selenium
print(selenium.__version__)
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!
I'm making an Instagram Bot (from a YT tutorial) and I can't get past the "Turn On Notifications" pop-up that appears after login.
I want to click "Turn On"
How do I click the button? Here's the xpath and what I see after inspection.
Xpath:
/html/body/div[2]/div/div/div/div[3]/button[1]
Inspection:
<button class="aOOlW bIiDR " tabindex="0">Turn On</button>
Here's my code. Can anyone tell me what to add and where? I'm attempting to click the button at the very bottom...
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
def print_same_line(text):
sys.stdout.write('\r')
sys.stdout.flush()
sys.stdout.write(text)
sys.stdout.flush()
class InstagramBot:
def __init__(self, username, password):
self.username = username
self.password = password
self.driver = webdriver.Firefox()
def closeBrowser(self):
self.driver.close()
def login(self):
driver = self.driver
driver.get("https://www.instagram.com/")
time.sleep(2)
login_button = driver.find_element_by_xpath("//a[#href='/accounts/login/?source=auth_switcher']")
login_button.click()
time.sleep(2)
user_name_elem = driver.find_element_by_xpath("//input[#name='username']")
user_name_elem.clear()
user_name_elem.send_keys(self.username)
passworword_elem = driver.find_element_by_xpath("//input[#name='password']")
passworword_elem.clear()
passworword_elem.send_keys(self.password)
passworword_elem.send_keys(Keys.RETURN)
time.sleep(2)
notify_button = browser.find_element_by_xpath('//button[text()="Turn On"]')
notify_button.click()
time.sleep(2)
Solution!
Instead of using this...
notify_button = browser.find_element_by_xpath('//button[text()="Turn On"]')
notify_button.click()
time.sleep(2)
Use this!
notify_element = driver.find_element_by_css_selector("COPY PASTE CSS SELECTOR HERE")
notify_element.send_keys(Keys.TAB)
time.sleep(1)
notify_element.send_keys(Keys.RETURN)
time.sleep(2)
Facebook Image along with its xpath
How to autopost my status after new Facebook layout using python and selenium... ?
Before this Update I had a perfect execution but now it says Xpath not found neither it is finding using css or tag property
Here is the code in through which I am accessing this field
for line in obj2:
#timeline
driver.find_element_by_xpath(".//*[#id='js_9a']/div[1]/div/div[1]/div[2]/div/div/div/div/div/div[2]/div/div/div/div").send_keys(line)
#post button
driver.find_element_by_xpath(".//[#id='js_9a']/div[2]/div[3]/div/div[2]/div/div[2]/button").click()
This should workaround the task. (If I find a better way to do it I will edit the code)
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
### login details ########################
username = "xxxxxxx#xxxxx.xxx"
pwd = "xxxxxxxxx"
##########################################
### what is on my mind ###################
msg = "hey there!"
##########################################
# initializing driver and loading web
chrome_path = r"chromedriver.exe"
options = Options()
options.add_argument("--disable-notifications")
driver = webdriver.Chrome(chrome_path, chrome_options=options)
driver.get("http://www.facebook.com/")
# end initializing
# start login
user_input = driver.find_element_by_xpath("""//*[#id="email"]""")
pwd_input = driver.find_element_by_xpath("""//*[#id="pass"]""")
user_input.send_keys(username)
pwd_input.send_keys(pwd)
pwd_input.send_keys(Keys.ENTER)
# end login
# writing msg
time.sleep(3)
first_what_is_on_my_mind_element = driver.find_element_by_class_name("_5qtp")
first_what_is_on_my_mind_element.click()
time.sleep(3)
second_what_is_on_my_mind_element = driver.switch_to.active_element
second_what_is_on_my_mind_element.send_keys(msg)
# end writing
# posting
buttons = driver.find_elements_by_tag_name('button')
for button in buttons:
if 'Post' in button.text:
button.click()
# end posting
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!'