I am working on a project, wherein I am trying to automate the recaptchas. It has been going good so far, however I have run into an issue. Here is the code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from selenium import *
from selenium.webdriver import *
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import requests
def initialize():
options = Options()
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
wait = WebDriverWait(driver, 20)
return driver, wait
def input_info():
name = driver.find_element(By.XPATH, "/html/body/div[3]/div/div/div[1]/form/div[1]/input")
name.send_keys("usernamer12392103821")
password = driver.find_element(By.XPATH, "/html/body/div[3]/div/div/div[1]/form/div[2]/input")
password.send_keys("Password123")
verify_password = driver.find_element(By.XPATH, "/html/body/div[3]/div/div/div[1]/form/div[3]/input")
verify_password.send_keys("Password123")
def select_recaptcha():
men_menu = wait.until(ec.visibility_of_element_located((By.XPATH, "/html/body/div[3]/div/div/div[1]/form/div[7]/div/div/div/iframe")))
men_menu.click()
def get_to_sound():
print("Searching for sound button")
time.sleep(3)
sound_butt_iframe = driver.find_element(By.XPATH, "/html/body/div[5]/div[4]/iframe")
print("Step one complete")
driver.switch_to.frame(sound_butt_iframe)
print("Step two complete")
sound_butt = sound_butt_iframe.find_element(By.XPATH, "/html/body/div/div/div[3]/div[2]/div[1]/div[1]/div[2]/button")
print("Step 4 complete")
driver.switch_to.default_content()
print("Step 5 complete")
sound_butt.click()
### set up driver to go to reddit page!
driver, wait = initialize()
driver.get('https://old.reddit.com/login')
time.sleep(5)
#### Fill in the information necessary! (username, password)
input_info()
time.sleep(3)
print("Finding recaptcha")
select_recaptcha()
print("looking to see if successful!")
try:
successful = wait.until(ec.visibility_of_element_located((By.XPATH, "/html/body/div[5]")))
print("Failed")
print("Call to API with sound file!")
try:
get_to_sound()
except:
print("Failed")
except:
print("Succeeded")
print("Carry on")
time.sleep(10000)
Basically, it will open the https://old.reddit.com/login webpage, enter in a dummy username and password (twice for the confirm) and then click on the recaptcha. This all works great. It realizes that the recaptcha "pops" up, and then it tries to click on the audio button, which is where I run into the problem. I think because it is "stuck" in a new iframe html tag, the XPATH isn't being registered, but I'm not sure. Does someone know how I can get the driver to press the audio control button? I've attached a screenshot below of the html stuff that would be needed for this.
It's the highlighted button tag at the bottom that I am "worried" about
Thanks in advance.
Edit: Here's the picture of the button I am trying to press. I took a picture of the whole recaptcha thing, and the button I am trying to press is the headphones at the bottom.
you should get iframe as element with driver.find_element(By.XPATH), and then find_element(By.XPATH internal) against that element
for that use switch_to_frame method
iframe = driver.find_element_by_xpath("//iframe[#title='...']")
driver.switch_to_frame(iframe)
# here access to internal elements by xpath
driver.find_element(By.XPATH, "")
driver.switch_to.default_content()
If NoSuchElementException accrued and xpath is right then the scroll to target button :
driver.execute_script('arguments[0].scrollIntoView(true);',
driver.find_element_by_xpath(XPATH OF Target button))
Related
I am trying to scrape LinkedIn website using Selenium. I can't parse Next button. It resists as much as it can. I've spent a half of a day to adress this, but all in vain.
I tried absolutely various options, with text and so on. Only work with start ID but scrape other button.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[#aria-label='Далее']"}
This is quite common for this site:
//*[starts-with(#id,'e')]
My code:
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from time import sleep
chrome_driver_path = Service("E:\programming\chromedriver_win32\chromedriver.exe")
driver = webdriver.Chrome(service=chrome_driver_path)
url = "https://www.linkedin.com/feed/"
driver.get(url)
SEARCH_QUERY = "python developer"
LOGIN = "EMAIL"
PASSWORD = "PASSWORD"
sleep(10)
sign_in_link = driver.find_element(By.XPATH, '/html/body/div[1]/main/p[1]/a')
sign_in_link.click()
login_input = driver.find_element(By.XPATH, '//*[#id="username"]')
login_input.send_keys(LOGIN)
sleep(1)
password_input = driver.find_element(By.XPATH, '//*[#id="password"]')
password_input.send_keys(PASSWORD)
sleep(1)
enter_button = driver.find_element(By.XPATH, '//*[#id="organic-div"]/form/div[3]/button')
enter_button.click()
sleep(25)
lens_button = driver.find_element(By.XPATH, '//*[#id="global-nav-search"]/div/button')
lens_button.click()
sleep(5)
search_input = driver.find_element(By.XPATH, '//*[#id="global-nav-typeahead"]/input')
search_input.send_keys(SEARCH_QUERY)
search_input.send_keys(Keys.ENTER)
sleep(5)
people_button = driver.find_element(By.XPATH, '//*[#id="search-reusables__filters-bar"]/ul/li[1]/button')
people_button.click()
sleep(5)
page_button = driver.find_element(By.XPATH, "//button[#aria-label='Далее']")
page_button.click()
sleep(60)
Chrome inspection of button
Next Button
OK, there are several issues here:
The main problem why your code not worked is because the "next" pagination is initially even not created on the page until you scrolling the page, so I added the mechanism, to scroll the page until that button can be clicked.
it's not good to create locators based on local language texts.
You should use WebDriverWait expected_conditions explicit waits, not hardcoded pauses.
I used mixed locators types to show that sometimes it's better to use By.ID and sometimes By.XPATH etc.
the following code works:
import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://www.linkedin.com/feed/"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href,'login')]"))).click()
wait.until(EC.element_to_be_clickable((By.ID, "username"))).send_keys(my_email)
wait.until(EC.element_to_be_clickable((By.ID, "password"))).send_keys(my_password)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
search_input = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[contains(#class,'search-global')]")))
search_input.click()
search_input.send_keys("python developer" + Keys.ENTER)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="search-reusables__filters-bar"]/ul/li[1]/button'))).click()
wait = WebDriverWait(driver, 4)
while True:
try:
next_btn = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "button.artdeco-pagination__button.artdeco-pagination__button--next")))
next_btn.location_once_scrolled_into_view
time.sleep(0.2)
next_btn.click()
break
except:
driver.execute_script("window.scrollBy(0, arguments[0]);", 600)
I'm trying to write Login credentials using send_keys element method with Selenium on this alert:
Login Alert
I get this alert after clicking on a button in a new window but I can't inspect the elements on this window.
here is my code
import time
import pynput
from selenium import webdriver
driver=webdriver.Chrome("C:/Users/dhias/OneDrive/Bureau/stgg/chromedriver.exe")
driver.get("http://10.15.44.177/control/userimage.html")
time.sleep(3)
window_before = driver.window_handles[0]
driver.maximize_window()
btn=driver.find_element_by_name("TheFormButton3")
btn.click()
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
obj = driver.switch_to.alert
time.sleep(2)
obj.send_keys('Admin')
time.sleep(2)
I get this error that tells me that there is no alert
I can't acess to the page so I can't try my code but, first I optimized It by adding WebdriverWait and the new By. function:
from selenium import webdriver as wd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = wd.Chrome("C:/Users/dhias/OneDrive/Bureau/stgg/chromedriver.exe")
wait = WebDriverWait(driver, 15)
driver.get("http://10.15.44.177/control/userimage.html")
driver.maximize_window()
window_before = driver.window_handles[0]
wait.until(EC.element_to_be_clickable((By.NAME, "TheFormButton3"))).click()
driver.switch_to.window(driver.window_handles[1])
alert = driver.switch_to.alert
alert.send_keys('Admin\npassword\n') # \n works as an ENTER key
You can try this code to see if the alert really exists or is an element of the webpage:
from selenium.common.exceptions import NoSuchElementException
def is_alert_present(self):
try:
self.driver.switch_to_alert().send_keys('Admin\npassword\n') # \n works as an ENTER key
print('Sucess')
except NoSuchElementException:
print('No Alert Present')
Also see Alerts in the Selenium Documentation
Also Try:
parent_h = browser.current_window_handle
# click on the link that opens a new window
handles = browser.window_handles # before the pop-up window closes
handles.remove(parent_h)
browser.switch_to_window(handles.pop())
# do stuff in the popup
# popup window closes
browser.switch_to_window(parent_h)
# and you're back
So finally I've found a solution..I used the approach where you have to send username and password in URL Request and it worked:
http://username:password#the-site.com
So I am trying to scrape a website, and I want to click an element, go to the page that opens from the click, find another element and click that one. The first click seems to work, no errors, but the next page doesn't open, thus I get an error. Here is a screenshot of what I want to click on the fist page: https://prnt.sc/10l8xa4. Clicking that should redirect to the second page. The problem seems to be that the driver clicks the element but nothing happens:
import sys, csv, os
from selenium import webdriver # Selenium 3.141.0
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
from datetime import datetime
from time import sleep
class Scraper(object):
''' A lot of the messy code is just playing with the tags from the page'''
def __init__(self, link):
self.link = link
self.driver = self.configure_driver() # The simulated browser
# Configuring the browser simulator, named driver, that will get all the information
def configure_driver(self):
# Add additional Options to the webdriver
chrome_options = Options()
# add the argument and make the browser Headless. It will work smoother& faster but it will miss the first category
# chrome_options.add_argument("--headless")
driver = webdriver.Chrome(options = chrome_options)
return driver
def click_element(self, selector): # Clicks the provided element from the page, even if not visible
element = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, selector)))
ActionChains(self.driver).move_to_element(element).click(element).perform()
if __name__ == '__main__':
product_link = 'https://www.action.com/nl-nl/click-and-collect-producten/' # An example of a product
app = Scraper(product_link)
with app.driver:
app.driver.get(product_link)
app.click_element('a.content-card.has-text.card-theme--light.card-size--s.card-align--bottom-left') # This gets clicked and should open new page, but it doesn't
sleep(10)
app.click_element('a.product-card__link') # This throws a Timeout, because the element can't be found, which is obvoius because the second page(which has this element) didn't open
sleep(20)
Try use like that:
with app.driver:
app.driver.get(product_link)
sleep(2)
app.click_element('li.has-submenu')
sleep(2)
app.click_element(
'div.grid-item.grid-item--content') # This gets clicked and should open new page, but it doesn't
sleep(2)
you should add step of openning pop-up and then click on your aimed button
Code with required argument but, by xpath:
def click_element(self, selector, by=By.CSS_SELECTOR): # Clicks the provided element from the page, even if not visible
element = WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((by, selector)))
ActionChains(self.driver).move_to_element(element).click(element).perform()
if __name__ == '__main__':
product_link = 'https://www.action.com/nl-nl/click-and-collect-producten/' # An example of a product
app = Scraper(product_link)
with app.driver:
app.driver.get(product_link)
sleep(2)
app.click_element("//section[#class='grid']/div[#class='grid-item grid-item--content'][1]", By.XPATH)
sleep(2)
I am trying to use selenium with python to automate some discord tasks. I have it open browser to discordapp.com, login, go to the server in question, but I can't find any css selector, xpath, class name, etc to be able to click on a channel called "commands". Any help would be appreciated.
from selenium import webdriver
import time
browser = webdriver.Firefox(executable_path=r'C:\Users\levir\OneDrive\Desktop\geckodriver.exe')
browser.get('https://discordapp.com')
linkElem = browser.find_element_by_class_name('appButton-3GZ9-9') #login button
linkElem.click()
linkElem = browser.find_element_by_xpath('/html/body/div/div[1]/div/div[2]/div/form/div/div/div/div[3]/div[1]/div/input')
linkElem.send_keys('EMAIL') #email
linkElem = browser.find_element_by_xpath('/html/body/div/div[1]/div/div[2]/div/form/div/div/div/div[3]/div[2]/div/input')
linkElem.send_keys('PASSWORD') #password
linkElem = browser.find_element_by_xpath('/html/body/div/div[1]/div/div[2]/div/form/div/div/div/div[3]/button[2]/div')
linkElem.click() #logs in
time.sleep(10)
linkElem = browser.find_element_by_xpath('/html/body/div/div[1]/div/div[2]/div/div/div[1]/div[2]/div[1]/div[6]')
linkElem.click() #enters server
time.sleep(1)
linkElem = browser.find_element_by_css_selector('div.containerDefault-1ZnADq:nth-child(4)')
linkElem.click() #is supposed to enter channel but doesn't work
The channels items I tried to click on had a class name of "wrapper-1BJsBx" and a tag type of "a".
Maybe "1BJsBx" is an autogenerated suffix so I propose using regex to avoid lots of maintenance.
I could click on specific channel using the following:
for elem in browser.find_elements_by_css_selector("a[class^=wrapper-]")
if "my_awesome_channel_name" in elem.get_attribute('aria-label').lower():
elem.click()
break
using python3.7 and selenium 3.141.0
The css selector is incorrect, go into the inspect panel, select the element, right click on it, select copy > copy selector and paste that selector
Copy the Xpath of this line and you'll be able to click it im pretty sure
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
import random
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(executable_path=PATH)
driver.get("https://discord.com/channels/191530268761260034/230742511235104769")
print(driver.title)
try:
sold = WebDriverWait(driver, 5).until(
EC.presence_of_element_located(
(By.XPATH, '/html/body/div/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/div[1]/div/div[2]/input'))
)
except:
pass
textName = driver.find_element_by_xpath('/html/body/div/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/div[1]/div/div[2]/input')
textName.send_keys('Put_Email_Here')
textCode = driver.find_element_by_xpath('/html/body/div/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/div[2]/div/input')
textCode.send_keys('Put_Code_Here')
#confirm button
confrim = driver.find_element_by_xpath('/html/body/div/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/button[2]').click()
#wating for the server to load
try:
sold = WebDriverWait(driver, 5).until(
EC.presence_of_element_located(
(By.XPATH,
'/html/body/div/div[2]/div/div[2]/div/div/div/div[2]/div[1]/nav/div[4]/div/div[5]/div[1]/div/div'))
)
except:
pass
channel1 = driver.find_element_by_xpath('/html/body/div/div[2]/div/div[2]/div/div/div/div[2]/div[1]/nav/div[4]/div/div[5]/div[1]/div/div').click()
Wrote simple python code to do the following:
1.login into linkedin
2. click search, then people
3. click "All Filters" to further modify search
I am getting stuck on step 3 and am unable to get driver to select and click on "All Filters"
I have tried to find_element by xpath, however it is dynamic and changes
I have tried to find_element by text "All Filters" and still doesn't work
For some reason when I right click on "All Filters" button and click Inspect, it first takes me to instead of the actual button; I have to right click - inspect a 2nd time to get to ; This makes me think the button is hidden and I am unsure of how to proceed.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import pynput.mouse as ms
import pynput.keyboard as kb
import time
driver = webdriver.Chrome("C:/Users/akame/Documents/PythonFiles/chromedriver.exe")
act = ActionChains(driver)
driver.get ('https://www.linkedin.com/')
driver.maximize_window()
login_email = driver.find_element_by_id('login-email')
login_password = driver.find_element_by_id('login-password')
submit1 = driver.find_element_by_id('login-submit')
mouse = ms.Controller()
keyboard = kb.Controller()
login_email.send_keys(‘XXXXX’)
login_password.send_keys('XXXXXX')
submit1.send_keys(u'\ue007')
searchfield = driver.find_element_by_xpath('//*[#id="ember49"]/input')
act.click(searchfield).perform()
act.send_keys(Keys.ARROW_DOWN).perform()
act.send_keys(Keys.ENTER).perform()
time.sleep(5)
#this is where it does not work--->
button_af = driver.find_element_by_text('All Filters')
button_af.click()
try code below, or just replace button_af = driver.find_element_by_text('All Filters') with button_af=find_element_by_xpath("//span[#class='artdeco-button__text' and text()='All Filters']"), cause sometime there's newline\r or \n in an element text, u should alway try to use contains instead of == to get an element's text:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
email = "ur_email"
pwd = "ur_pwd"
driver = webdriver.Chrome()
act = ActionChains(driver)
driver.get ('https://www.linkedin.com/')
login_email = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "login-email"))
)
login_password = driver.find_element_by_id('login-password')
submit1 = driver.find_element_by_id('login-submit')
login_email.send_keys(email)
login_password.send_keys(pwd)
submit1.click()
searchfield = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[#role='combobox']")))
searchfield.click()
searchKeywordXpath = "//span[text()='People']"
searchKeyword = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, searchKeywordXpath)))
searchKeyword.click()
btnAllFilterXpath = "//span[#class='artdeco-button__text' and text()='All Filters']"
btnAllFilter = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, btnAllFilterXpath)))
btnAllFilter.click()
# just debug code: wait to see the right page appear or not
time.sleep(10)