I'm trying to use Selenium to access data on the next page. For some reason, I can't get it to click submit on the web page: https://www.clarkcountycourts.us/Portal/Home/Dashboard/29
my code is as followed:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(20) # gives an implicit wait for 20 seconds
driver.get("https://www.clarkcountycourts.us/Portal/Home/Dashboard/29")
search_box = driver.find_element_by_id("caseCriteria_SearchCriteria")
search_box.send_keys("Robinson")
WebDriverWait(driver, 15).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//span[#id='recaptcha-anchor']"))).click()
submit_box = driver.find_element_by_id("btnSSSubmit").click()
I get the error
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="btnSSSubmit"]"}
for the last line of code submitting any assistance would be greatly appreciated.
the button elements are as followed:
<input name="Search" id="btnSSSubmit" class="btn btn-primary pull-right" value="Submit" type="submit">
In your code you are switching into the iframe and accessing an element inside it.
But the submit button is not inside that iframe, so to continue with elements out of that iframe you have to switch to the default content.
This should work better:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(20) # gives an implicit wait for 20 seconds
driver.get("https://www.clarkcountycourts.us/Portal/Home/Dashboard/29")
search_box = driver.find_element_by_id("caseCriteria_SearchCriteria")
search_box.send_keys("Robinson")
WebDriverWait(driver, 15).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.XPATH, "//span[#id='recaptcha-anchor']"))).click()
driver.switch_to.default_content()
submit_box = driver.find_element_by_id("btnSSSubmit").click()
Related
<input type="submit" value="Example" id="free_button_form_button" class="free_swag_element new_button_style profile_page_button_style">
I'm trying to have selenium click the button but that is all the website uses and I don't know how to make it click.
You can try:
from selenium import webdriver
url = "http://websitethatyouwanttoscrape.com"
driver = webdriver.Firefox()
driver.get(url)
button = driver.find_element_by_id('free_button_form_button')
button.click()
Or if you need to wait until the button is clickable, you can:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
url = "http://websitethatyouwanttoscrape.com"
driver = webdriver.Firefox()
driver.get(url)
wait = WebDriverWait(driver, 10)
button = wait.until(EC.element_to_be_clickable((By.ID, 'free_button_form_button')))
ActionChains(driver).click(button).perform()
i want to click on button that means next and it wrote by 'بعدی'
in this page
https://www.tgju.org/profile/price_dollar_rl/history
here is my code
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument("--headless")
browser= webdriver.Firefox(options=options, executable_path="geckodriver.exe")
browser.get('https://www.tgju.org/profile/price_dollar_rl/history');
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
wait = WebDriverWait(browser,20)
from selenium.webdriver.common.by import By
browser.find_element_by_xpath('//*[#id="DataTables_Table_0_next"]')
and I get this error
Unable to locate element: //*[#id="DataTables_Table_0_next"]
but i copy exact id form inspect
thanks
You need to add a wait for element to be present. On this page it looks like it's dynamically loaded.
element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "DataTables_Table_0_next"))
)
Here's inspected source code
input aria-label="Phone number, username, or email" aria-required="true" autocapitalize="off" autocorrect="off" maxlength="75" name="username" type="text" class="_2hvTZ pexuQ zyHYP" value=""
I have tried this code run
driver = webdriver.Chrome()
driver.get('https://www.instagram.com/')
driver.find_element_by_xpath("//input[#name=\"username\"]").send_keys(username)
driver.find_element_by_xpath("//input[#name=\"password\"]").send_keys(pw)
driver.find_element_by_xpath('//button[#type="submit"]').click()
But having error like this
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#name="username"]"}
(Session info: chrome=83.0.4103.61)
My chromedriver and chrome version are match, and finding elements by following instruction. Why am I getting this error?
Instagram application is built through React elements. Hence just after invoking the url when you initiate the search for the login element, you face NoSuchElementException
Solution
To login within Instagram using a valid set of credentials you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:
Using XPATH:
driver.get("https://www.instagram.com/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='username']"))).send_keys("username")
driver.find_element_by_xpath("//input[#name='password']").send_keys("password")
driver.find_element_by_xpath("//button/div[text()='Log In']").click()
Note : You have to add the following imports:
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:
Reference
You can find a couple of relevant discussions in:
Filling in login forms in Instagram using selenium and webdriver (chrome) python OSX
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
Observe that while open instagram homepage, it shows spinner on login form for few moment and then display the fields. So your need to manage synchronization in your script.
Use explicit wait in your code until desired field get ready for interaction.
username = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[#name='username']")))
username.send_keys('username')
password = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[#name='password']")))
password.send_keys('pw')
Need to import below packages
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Try the below code:
driver = webdriver.Chrome()
driver.get('https://www.instagram.com/')
txt_user = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, 'username')))
txt_user.send_keys('yourUserName')
txt_pwd = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, 'password')))
txt_pwd.send_keys('yourPassword')
btn_submit = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[type="submit"]')))
btn_submit.click()
Following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
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()
driver.get('https://www.instagram.com/')
element = WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.ID, "//input[#name=\"username\"]"))
)
element.sendkeys('user')
Can someone help me understand why my code fails to find the element by ID. Code below:
from selenium import webdriver
driver=webdriver.Firefox()
driver.get('https://app.waitwhile.com/checkin/lltest3/user')
element = driver.find_element_by_id("guestPhone")
Inspecting element shows the ID clearly.
<input type="tel" name="guestPhone" id="guestPhone" class="form-control ng-pristine ng-empty ng-invalid ng-invalid-phone-validator ng-invalid-required ng-touched" ng-model="form.model" ng-model-options="{ 'updateOn': 'default blur', 'debounce': { 'default': 350, 'blur': 0 } }" uib-typeahead="guest.phone for guest in form.onChange({value:$viewValue})" typeahead-min-length="6" typeahead-on-select="form.onSelect({guest:$item})" typeahead-select-on-exact="true" uib-tooltip="Please enter valid number. Include country code for non-US numbers" tooltip-trigger="'none'" tooltip-is-open="(form.guestForm.$submitted || form.guestForm.guestPhone.$touched) && form.guestForm.guestPhone.$invalid" tooltip-placement="bottom" ng-required="::form.required" phone-validator="US" placeholder="Mobile phone" title="Please enter a valid phone number" autocomplete="nope" next-on-enter="" aria-autocomplete="list" aria-expanded="false" aria-owns="typeahead-47-2884" required="required" style="">
P.S. I've also tried XPath and name as well. Still no luck.
You need to wait for the element to become visible on the page. You can tell this is loaded in dynamically because if you right-click on the page in chrome and view source you'll see there's no guestPhone element. It gets loaded in with javascript
Here's an example from http://isaacviel.name/make-web-driver-wait-element-become-visiable/:
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.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
You can try with web driver wait :
Code :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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(executable_path = r'D:/Automation/chromedriver.exe')
driver.maximize_window()
driver.get("https://app.waitwhile.com/checkin/lltest3/user")
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'guestPhone')))
element.send_keys('006867987')
This is my code:
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
browser = webdriver.PhantomJS()
browser.set_window_size(1120, 550)
browser.get("http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html")
username = browser.find_element_by_id("navbar_username")
password = browser.find_element_by_name("vb_login_password_hint")
username.send_keys("user")
password.send_keys("password")
browser.find_element_by_class_name("loginbutton").click()
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, '//h2[contains(., "Redirecting")]')))
wait.until(EC.title_contains('Kenyan & Tanzanian'))
link = browser.find_element_by_xpath('//div[#class="vbseo_liked"]/a[contains(#onclick, "return vbseoui.others_click(this)")]')
link.click()
browser.save_screenshot('screenie.png')
print 'success!!'
browser.close()
For this HTML code:
<div class="vbseo_liked">
Nyaralego
,
Sikonge
,
Ab-Titchaz
and
<a onclick="return vbseoui.others_click(this)" href="http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html#">11 others</a>
like this.
</div>
I want to be able to click on this link:
<a onclick="return vbseoui.others_click(this)" href="http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html#">11 others</a>
And then take a screenshot of the page after it has been clicked. This error I keep getting though when i run the code.
selenium.common.exceptions.NoSuchElementException: Message: {"errorMessage":"Unable to find element with class name 'vbseo_liked'"
You need to wait for the list of posts to load before making a click:
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
browser = webdriver.PhantomJS()
browser.maximize_window()
browser.get("http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html")
username = browser.find_element_by_id("navbar_username")
password = browser.find_element_by_name("vb_login_password_hint")
username.send_keys("username")
password.send_keys("password")
browser.find_element_by_class_name("loginbutton").click()
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, '//h2[contains(., "Redirecting")]')))
wait.until(EC.title_contains('Kenyan & Tanzanian'))
wait.until(EC.visibility_of_element_located((By.ID, 'postlist')))
link = browser.find_element_by_xpath('//div[#class="vbseo_liked"]/a[contains(#onclick, "return vbseoui.others_click(this)")]')
link.click()
browser.save_screenshot('screenie.png')
print 'success!!'
browser.close()
Note that the generated screenshot would be very large (about 39 MB on disk).