How to Autopost Facebook status (after new layout) using Python and selenium - python

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

Related

Webdriver ( Selenium ) Cannot find the element

`
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 can't get pass recaptcha with selenium infinitely stuck

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

Selenium cant use the find_element_by_css_selector()

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__)

Trying to sign into google using a selenium python script

I am using selenium to open and sign into google accounts as my first step. I have successfully opened and filled the email response although upon submitting I receive the error of
"This browser or app may not be secure. Learn more Try using a
different browser. If you’re already using a supported browser, you
can refresh your screen and try again to sign in." From google.
Is there any way to get around this? Here is my code below.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://accounts.google.com/")
print(driver.title)
search = driver.find_element_by_name("identifier")
search.send_keys("email goes here")
search.send_keys(Keys.RETURN)
Was having the same issue and i found this thread in GitHub.
The solution that worked for me was to use this driver: undetected_chromedriver instead of the normal ChromeDriver.
import undetected_chromedriver.v2 as uc
chrome_options = uc.ChromeOptions()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("user_agent=DN")
self.browser = uc.Chrome(options=chrome_options)
self.browser.delete_all_cookies()
# example of loggin in to youtube without getting that issue
self.browser.get('http://youtube.com')
login_button_init = self.browser.find_element_by_xpath("//a[#aria-label='Sign in']")
login_button_init.click()
# locate the login button
login_button = self.browser.find_element_by_xpath("//paper-button[#aria-label='Sign in']")
login_button.click()
# get email and set to email input box
email = self.browser.find_element_by_id("identifierId")
myemail = os.environ.get('YOUTUBE_EMAIL')
email.send_keys(myemail)
# click next button
email_next_button = self.browser.find_element_by_id("identifierNext")
email_next_button.click()
# get password and set to password input box
password = self.browser.find_element_by_name("password")
mypassword = os.environ.get('YOUTUBE_PASSWORD')
password.send_keys(mypassword)
sleep(2)
# click next button to log in
pass_next_button = self.browser.find_element_by_id("passwordNext")
pass_next_button.click()

Can't scroll down in youtube (My code can run some website, but not with Youtube) using Selenium

My code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# execute url
url = "https://www.youtube.com/user/xuanvinh1612/community"
driver_path = ('F:/chromedriver.exe')
browser = webdriver.Chrome(executable_path=driver_path)
browser.get(url)
# Auto scroll and auto click with text:'Read more'
read_mores2 = browser.find_elements_by_link_text('Read more')
for read_mores2 in read_mores2:
browser.execute_script("arguments[0].scrollIntoView();", read_mores2)
browser.execute_script("$(arguments[0]).click();", read_mores2)
# Scroll down stop when all post was showed
read_mores2 = browser.find_elements_by_link_text('Read more')
With a same code, my code can run some website(2-3 another website). But when i re-use code for auto scroll down and auto click on Youtube/community, it not working. I dont know how it not work. I need help, please.
Try this code:
It will first load all the pages, then click on all Read More.
import time
from selenium import webdriver
# execute url
url = "https://www.youtube.com/user/xuanvinh1612/community"
browser = webdriver.Chrome()
browser.get(url)
# Auto scroll and auto click with text:'Read more'
previous_count = 0
page_sections = browser.find_elements_by_css_selector('.style-scope.ytd-item-section-renderer')
current_count = len(page_sections)
print("Scrolling to enable all the pages")
while previous_count != current_count:
try:
previous_count = current_count
browser.execute_script("arguments[0].scrollIntoView();", page_sections[-1])
print("Number of total Elements found: {}".format(len(page_sections)))
finally:
# As the page load the newer elements, you need to implement logic here to wait until the loading spinner at the
# button becomes invisible (not attached to the DOM)
time.sleep(2) # WorkAround as you need to implement the above logic here
page_sections = browser.find_elements_by_css_selector('.style-scope.ytd-item-section-renderer')
current_count = len(page_sections)
print("Clicking on all Read More")
for read_more in browser.find_elements_by_css_selector('.more-button'):
browser.execute_script("arguments[0].scrollIntoView();", read_more)
browser.execute_script("arguments[0].click();", read_more)

Categories