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__)
Related
`
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from time import sleep
import os
login_page = "https://fap.fpt.edu.vn/Default.aspx"
# page = "https://fap.fpt.edu.vn/Report/ScheduleOfWeek.aspx"
email = ""
password = ""
options = Options()
options.add_argument("--window-size=1920,1080")
options.binary_location = "C:\Program Files\Google\Chrome\Application\chrome.exe"
driver = webdriver.Chrome(options = options)
driver.get(login_page)
login1 = driver.find_element("xpath","//div[#class='abcRioButtonContentWrapper']").click()
driver.find_element(By.NAME, "identifier").send_keys(email)
sleep(3)
driver.find_element(By.ID, "identifierNext").click()
sleep(2)
driver.find_element(By.NAME, "password").send_keys(password)
sleep(2)
driver.find_element(By.ID, "passwordNext").click()
sleep(999999)
`
I believe i chose the right By.NAME ( "indentifier" ) but the code still not work and return message no such element.
I tried to change the syntax, using xpath change By.NAME to By.ID but it still not work
Google login page opens in a new window. So you need to switch to this window before interacting with it. So you need to use this code (the first and the last lines are from your code and between is the part you need to add):
login1 = driver.find_element("xpath","//div[#class='abcRioButtonContentWrapper']").click()
windows = driver.window_handles
driver.switch_to.window(windows[1])
driver.find_element(By.NAME, "identifier").send_keys(email)
And after you've finished with the login you will need to switch back to the main window. To do that you can use this code:
driver.switch_to.window(windows[0])
And then work with the content of the page
No, don't use name of jsname here, because they get filled with random data.
Just find your Google account name or your email address by text and click it:
userAccount = driver.find_element_by_xpath("//*[text()='YourGoogleAccountName']")
userAccount.click()
I am working on a auto form submitting project I tried selenium but I could fill the forms but when I click submit button a recaptcha being summoned, when I tried to bypass it with 2captcha but didn't work and after that I tried to do it manually but when I do it correctly it gets reseted can you help me with my code?
import time
import os
import requests
import random
import string
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver as wd
import sys
def random_char(y):
return ''.join(random.choice(string.ascii_letters) for x in range(y))
mail = (random_char(7)+"#gmail.com")
driver = wd.Firefox(executable_path='C:\geckodriver.exe')
target_url = 'https://www.yemeksepeti.com/login/new?step=registration'
driver.get(target_url)
driver.maximize_window()
time.sleep(2)
print(mail)
time.sleep(2)
email = str(mail)
time.sleep(2)
firstName = 'Mert'
time.sleep(2)
lastName = 'Demir'
time.sleep(2)
birthDate = '01-04-2001'
time.sleep(2)
password = 'AbcAbc123.*_123'
time.sleep(2)
emailPath = driver.find_element("xpath", '//*[#id="email"]')
time.sleep(2)
emailPath.send_keys(mail)
time.sleep(2)
firstNamePath = driver.find_element("xpath", '//*[#id="first_name"]')
time.sleep(2)
firstNamePath.send_keys(firstName)
time.sleep(2)
lastNamePath = driver.find_element("xpath", '//*[#id="last_name"]')
time.sleep(2)
lastNamePath.send_keys(lastName)
time.sleep(2)
birthDatePath = driver.find_element("xpath", '//*[#id="birthdate"]')
time.sleep(2)
birthDatePath.send_keys(birthDate)
time.sleep(2)
passwordPath = driver.find_element("xpath", '//*[#id="password"]')
time.sleep(2)
passwordPath.send_keys(password)
time.sleep(2)
Submit = driver.find_element("xpath", '//*[#id="login-page-react-root"]/main/div/form/div[8]/button')
Submit.click()
time.sleep(2)
The main reason for having a re-captcha is to stop bots (ie: selenium automation scripts) from interacting with the web page/form.
If you can bypass that this is a big red light for the application security. And is not recommended in standard QA practices.
What you need to do is to have your dev environment configured to be test friendly. There are multiple ways this can be achieved. From my personal experience :
Remove the captcha field for dev/test environment forms.
Have the developers setup a static captcha which always has one known captcha phrase
Test the feature with the captcha manually
I'm sure there are a lot of other ways to overcome this situation, but forcing the script to enter the real captcha is not the way to go
I am trying to click on the first result on this page, but all the options I tried didn't work.
Firstly I just login into the website with email: kocianlukyluk#gmail.com and password: Redfinpython06. Here is the code for it:
driver = webdriver.Chrome("C:\\Users\\kocia\\OneDrive\\Plocha\\Python\\nastaveni\\chromedriver.exe")
driver.get('https://www.redfin.com/myredfin/favorites')
email = 'kocianlukyluk#gmail.com'
password = 'Redfinpython06'
time.sleep(3)
driver.find_element_by_xpath(
'//*[#id="content"]/div[6]/div/div[2]/div/div/form/span[1]/span/div/input').send_keys(email)
time.sleep(3)
driver.find_element_by_xpath(
'//*[#id="content"]/div[6]/div/div[2]/div/div/form/span[2]/span/div/input').send_keys(password)
time.sleep(3)
sing_up = driver.find_element_by_css_selector('button[type=submit]')
sing_up.click()
But the problem is after login i can't click on the first result on the page.
Here is what i tried:
result = driver.find_elements_by_xpath("//*[#id="content"]/div[10]/div/div[5]/div/div[2]/div/div")[0]
result.find_element_by_xpath("//*[#id="content"]/div[10]/div/div[5]/div/div[2]/div/div/div[1]").click()
or
result = driver.find_elements_by_xpath("//*[#id="content"]/div[10]/div/div[5]/div/div[2]/div/div")[0]
result.click()
or
result = driver.find_element_by_xpath("//*[#id="content"]/div[10]/div/div[5]/div/div[2]/div/div/div[1]")
result.click()
Thank you so much for help.
I hope that is a dummy email and password that you are just using for testing purposes :)
Below clicks on the first house picture in the list. I also cleaned up your email and password xpath designations. You can see how much easier it is to grab them by name
Also, you may want to put proper wait methods around these find elements. Using sleep generally is not recommended
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
driver = webdriver.Chrome()
driver.get('https://www.redfin.com/myredfin/favorites')
email = 'kocianlukyluk#gmail.com'
password = 'Redfinpython06'
sleep(3)
driver.find_element_by_name(
'emailInput').send_keys(email)
sleep(3)
driver.find_element_by_name(
'passwordInput').send_keys(password)
sleep(3)
sing_up = driver.find_element_by_css_selector('button[type=submit]')
sing_up.click()
sleep(3)
first_house = driver.find_element_by_xpath("//div[#class='FavoritesHome'][1]//img")
first_house.click()
I'm trying Python and Selenium. My goal is to log myself into Discord (https://discordapp.com/login. But here is the problem. I can't manage to get the email and password box selected. But the worst part is trying to select a textbox on a server... I tried everything, even locating by XPath, but I can't seem to do it right. Also, doing it on ATOM is probably not the best idea since I don't get any error messages :P. Here is a snippet to select the email textbox.
from selenium
import webdriver
from selenium.webdriver.common.keys
import Keys
browser = webdriver.Firefox()
browser.get('https://discordapp.com/login')
assert 'discordapp' in browser.title
elem = browser.find_element_by_name('textarea')# this is the part where i need help
elem.send_keys('test' + Keys.ENTER)
For email this css selector should work :
input[type='email']
For password :
input[type='password']
I've tested this code :
browser.get("https://discordapp.com/login")
elem = browser.find_element_by_css_selector("input[type='email']")# this is the part where i need help
elem.send_keys("itsolidude#imail.com")
elem1 = browser.find_element_by_css_selector("input[type='password']")# this is the part where i need help
elem1.send_keys("password")
login_button = browser.find_element_by_xpath("//div[text()='Login']/parent::button")
login_button.click()
This worked fine on my machine.
you need to check the div container and add them into the xpath.
Try the following code and please debug the indents, in case that stackoverflow is not transferring them properly (well, I don't know how to do it nice and correctly.)
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
class loginPage():
def test(self):
baseUrl = 'https://discordapp.com/login'
driver = webdriver.Firefox(executable_path="G:\\webdriver/geckodriver.exe")
driver.maximize_window()
driver.implicitly_wait(5)
driver.get(baseUrl)
mail = driver.find_element(By.XPATH, "//div[3]/div[1]/div/input[contains(#type,'email')]")
time.sleep(5)
mail.send_keys("test#gmail.com")
time.sleep(3)
print("Enter mail adress")
password = driver.find_element(By.XPATH, "//div[3]/div[2]/div/input[contains(#type,'password')]")
time.sleep(5)
password.send_keys("123456789")
time.sleep(3)
print("Enter password")
time.sleep(10)
driver.quit()
ff = loginPage()
ff.test()
Login To Discord Website using Python and Selenium:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome()
driver.get("https://discord.com/login")
time.sleep(6)
username_input = driver.find_element_by_name('email')
username_input.send_keys("enter-your-username-here")
password_input = driver.find_element_by_name('password')
password_input.send_keys("Enter-your-password-here")
login_button = driver.find_element_by_xpath('//*[#id="app-mount"]/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/button[2]')
login_button.click()
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