I want to, click on the button to resolve the captcha through the audio, but selenium does not detect the specified "id".
browser.get("https://www.google.com/recaptcha/api2/demo")
mainWin = browser.current_window_handle
iframe = browser.find_elements_by_tag_name("iframe")[0]
browser.switch_to_frame(iframe)
CheckBox = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID ,"recaptcha-anchor"))).click()
sleep(4)
audio = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID ,"recaptcha-audio-button"))).click()
To click() on the button to resolve the captcha through the audio as the desired elements are within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following Locator Strategies:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.google.com/recaptcha/api2/demo")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.google.com/recaptcha/api2/anchor']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()
driver.switch_to.default_content()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-audio-button"))).click()
Browser Snapshot:
Reference
Ways to deal with #document under iframe
Outro
You can find a couple of relevant discussions in:
How to click on the reCaptcha using Selenium and Java
CSS selector for reCaptcha checkbok using Selenium and vba excel
Find the reCAPTCHA element and click on it — Python + Selenium
Very useful, just put your attention, that text: 'recaptcha challenge' in selector below depends from regional settings/language:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))
Related
I have been trying to create account within Instagram Sign up page with selenium and python, and I am able to enter all info into the text boxes, but for some reason, I am unable to click the "Sign up" button. I have tried using find element by XPath,CSS,ID, and class name but python still says that it cannot find the element. This is after entering all other necessary info on the form. Does anyone have any ideas?
The desired elements is a React element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.instagram.com/accounts/emailsignup/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='emailOrPhone']"))).send_keys("9876543210")
driver.find_element_by_xpath("//input[#name='fullName']").send_keys("aforkman")
driver.find_element_by_xpath("//input[#name='username']").send_keys("aforkman")
driver.find_element_by_xpath("//input[#name='password']").send_keys("aforkman")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Sign up']"))).click()
Browser Snapshot:
You can use this way :
driver.find_element_by_xpath("//button[contain(text() , "Sign up")]").click()
I want to, click on the button to resolve the captcha through the audio, but selenium does not detect the specified "id".
browser.get("https://www.google.com/recaptcha/api2/demo")
mainWin = browser.current_window_handle
iframe = browser.find_elements_by_tag_name("iframe")[0]
browser.switch_to_frame(iframe)
CheckBox = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID ,"recaptcha-anchor"))).click()
sleep(4)
audio = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID ,"recaptcha-audio-button"))).click()
To click() on the button to resolve the captcha through the audio as the desired elements are within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following Locator Strategies:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.google.com/recaptcha/api2/demo")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.google.com/recaptcha/api2/anchor']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()
driver.switch_to.default_content()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-audio-button"))).click()
Browser Snapshot:
Reference
Ways to deal with #document under iframe
Outro
You can find a couple of relevant discussions in:
How to click on the reCaptcha using Selenium and Java
CSS selector for reCaptcha checkbok using Selenium and vba excel
Find the reCAPTCHA element and click on it — Python + Selenium
Very useful, just put your attention, that text: 'recaptcha challenge' in selector below depends from regional settings/language:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))
I'm trying to scrape this website
https://maroof.sa/BusinessType/BusinessesByTypeList?bid=14&sortProperty=BestRating&DESC=True
There is a button to load more content when you click it, it displays more content without changing the URL
I had made a piece of code to load all the content first then extract all the URLs of the data I need then go to each link and scrape the data
url = "https://maroof.sa/BusinessType/BusinessesByTypeList?bid=26&sortProperty=BestRating&DESC=True"
driver = webdriver.Chrome()
driver.get(url)
# button = driver.find_element_by_xpath('//*[#id="loadMore"]/button')
num = 1
while num <= 507:
sleep(4)
button = WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="loadMore"]/button')))
button.click()
print(num)
num += 1
links = [l.get_attribute('href') for l in WebDriverWait(driver, 40).until(EC.visibility_of_all_elements_located((By.XPATH, '//*[#id="list"]/a')))]
it seems to work but sometimes it doesn't click on the button that loads the content it accidentally
click on something else and makes an error and i have to start over again
Can you help me?
To scrape the website clicking on the button to load more content you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://maroof.sa/BusinessType/BusinessesByTypeList?bid=26&sortProperty=BestRating&DESC=True')
while True:
try:
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//button[#class='btn btn-primary']"))))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-primary']"))).click()
except TimeoutException:
break
print([l.get_attribute('href') for l in WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, '//*[#id="list"]/a')))])
driver.quit()
Im trying to find the username path for the login page on https://www.textnow.com/login. I've tried finding it by x_path, ID, Name, class but my bot just cant find it. Does anyone have any possible solutions that I would be able to try out ?
Source Code:
"SUDO FUNCTION: OPEN A NEW TAB FOR TEXT NOW AND LOG IN"
driver.implicitly_wait(3)
driver.execute_script("window.open('http://www.textnow.com/login','new window')")
textNowEmail = driver.find_element_by_id('txt-username')# still have not found username textfield
textNowEmail.send_keys(textNowUser)
#Set password code
textNowPass = driver.find_element_by_id('txt-password')
textNowPass.send_keys('fill')
This is the message im getting:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="txt-username"]"}
(Session info: chrome=78.0.3904.108)
To send a character sequence to the Email or Username and Password fields with in the website https://www.textnow.com/login you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://www.textnow.com/login")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.uikit-text-field__input#txt-username"))).send_keys("Xavier-Uriel-Espinal")
driver.find_element_by_css_selector("input.uikit-text-field__input#txt-password").send_keys("Xavier-Uriel-Espinal")
Using XPATH:
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://www.textnow.com/login")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='uikit-text-field__input' and #id='txt-username']"))).send_keys("Xavier-Uriel-Espinal")
driver.find_element_by_xpath("//input[#class='uikit-text-field__input' and #id='txt-password']").send_keys("Xavier-Uriel-Espinal")
Note : You have to add the following imports :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
You are opening a new window. Are you switching to it ? To make sure you are on the right window you can get page source by using "driver.get_source()" method and then evaluate the DOM.
Considering there are 2 window handles, you can switch to newly opened window using following :
required_window = driver.window_handles[1]
driver.switch_to_window(required_window)
Also try using "WebDriverWait" and "expected_conditions" to wait till required element is present by importing following:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
and then finding element using expected conditions:
WebDriverWait(driver,5).until(
EC.presence_of_element_located((By.ID, "txt-username")))
When you are opening new window with execute_script your window handle is still in the original window. You need to switch window.
You can check all the windows available with driver.window_hanldes
For your case just use
driver.switch_to.window(driver.window_handles[1])
after opening new window
then proceed with rest of your code
I'm trying to scrape this page. Before getting into the page listings, a Select Location window pops up, so I'm trying to tell selenium to click two buttons in order to access the product listings.
Problem is, Selenium is not able to locate the xpath I'm using to locate this two buttons!
Here's my code:
from selenium import webdriver
driver = webdriver.Chrome("webdriver/chromedriver.exe")
driver.implicitly_wait(30)
driver.get("https://www.indiacashandcarry.com/shop/HomestyleFood")
locationButton = driver.find_element_by_xpath('//*[#id="location-list"]/li[1]/h4/a')
groceriesButton = driver.find_element_by_xpath('//*[#id="price-list-0"]/ul/li[1]')
locationButton.click()
groceriesButton.click()
Here's the site:
https://www.indiacashandcarry.com/shop/HomestyleFood
I'm thinking it is because this popup is on other type of frame, but I couldn't find any iframe index, so I'm a bit lost. Please help!
Your xpath looks fine.Use Webdriverwait to handle dynamic element.
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
driver = webdriver.Chrome("webdriver/chromedriver.exe")
driver.get("https://www.indiacashandcarry.com/shop/HomestyleFood")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="location-list"]/li[1]/h4/a'))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//*[#id="price-list-0"]/ul/li[1]'))).click()
On the website https://www.indiacashandcarry.com/shop/HomestyleFood first to click() on Select This Location associated with FREMONT and then click() on Groceries you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following solution:
Code Block:
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
# options.add_argument('disable-infobars')
options.add_argument('--disable-extensions')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://www.indiacashandcarry.com/shop/HomestyleFood")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h4[contains(., 'Fremont')]/a"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h5[#class='mtopbot5 ng-binding' and contains(., 'Groceries')]"))).click()
Browser Snapshot: