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 am really new to selenium.
Currently, I am trying to use both selenium and beautifulsoup to do some webcrawling. The website that I am webcrawling on is https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp.
this is the code that I have for now.
driver = webdriver.Chrome(executable_path=path_to_chromebrowser)
driver.get("https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp")
input_area = driver.find_element_by_name("searchForm.genename")
input_area.send_keys("P2RY12")
searcher = driver.find_element_by_class_name("button")
searcher.click()
# table = driver.find_element_by_class_name("table7 table7-border")
# table.find_element_by_tag_name("a").click()
I am trying to click the first SNP ID that comes up, upon search. What would be the good way for me to click the href of the search result?
ON the webpage https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp to search for the Gene Name as P2RY12 and click the first SNP ID that comes up upon search you need to induce WebDriverWait for the element_to_be_clickable() and 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://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#idgname[name='searchForm.genename']"))).send_keys("P2RY12")
driver.find_element_by_css_selector("button.button[type='submit']").click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form[action^='/dogsdv2/com/exportFile'] table>tbody>tr td:nth-child(3)>a"))).click()
Browser Snapshot:
Try this:
firstsnpID = driver.find_element_by_xpath("(.//table[#class='table7 table7-border']/tbody/tr/td[3]/a)[1]")
firstsnpID.click()
you can not use compound classes to locate element using find_element_by_class_name
driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/form/table/tbody/tr[1]/td[3]/a[1]').click()
If you need other ids:
for id in range(1,10):
driver.find_element_by_xpath('/html/body/div/div[2]/div[2]/form/table/tbody/tr[{}]/td[3]/a[1]'.format(id)).click()
sleep(5)
driver.back()
To click on first link on the table induce WebDriverWait() and element_to_be_clickable() and following CSS selector.
Code:
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
driver = webdriver.Chrome(executable_path=path_to_chromebrowser)
driver.get("https://bigd.big.ac.cn/dogsdv2/pages/modules/indsnp/indsnp_search.jsp")
input_area = driver.find_element_by_name("searchForm.genename")
input_area.send_keys("P2RY12")
searcher = driver.find_element_by_class_name("button")
searcher.click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"table.table7.table7-border td>a[href^='/dogsdv2/refsnp/showRefSNPDetail']"))).click()
To get all the link induce WebDriverWait() and visibility_of_all_elements_located() and get the href value then iterate each url
allelemets=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"table.table7.table7-border td>a[href^='/dogsdv2/refsnp/showRefSNPDetail']")))
allurls=[item.get_attribute('href') for item in allelemets]
print(allurls)
for link in allurls:
driver.get(link)
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: