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')
Related
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()
<button name="main" type="submit" class="mainfont">
التسجيل</button>
XPath:
/html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table[1]/tbody/tr/td[18]/font/button
Full XPath:
/html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table[1]/tbody/tr/td[18]/font/button
How can I click this button in selenium? I have tried different methods but none worked, some notes:
There are different submit buttons called "main" with mainfont class
I tried using the XPath but the function also requires me to use the text inside the button, which is in arabic characters that split into two lines in python text editor, which ofcourse does not work as it needs to be in 1 line, but if I erase the space and make it into one code line, it won't work (probably because it is written in two lines in the HTML code?)
Edit: added code
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
#
path = "XXXX"
driver = webdriver.Chrome(path)
#
driver.get("XXXX")
driver.maximize_window()
#
login1 = driver.find_element_by_name("liun")
login1.send_keys("XXXX")
time.sleep(2)
login1.send_keys(Keys.RETURN)
#
try:
element1 = WebDriverWait(driver, 100).until(
EC.presence_of_element_located((By.title_is, "XXXX"))
)
finally:
login2 = driver.find_element_by_name("gsw")
login2.send_keys("XXXX")
login2.send_keys(Keys.RETURN)
pass
# THE CODE BELOW DOES NOT WORK
html = '''
<button name="main" type="submit" class="mainfont">\nالتسجيل</button>
'''
driver.get("data:text/html;charset=utf-8," + html)
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='\nالتسجيل']"))).click()
I made the HTML that you've provided and could create a xpath with text and clicked on it using Explicit waits :
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
html = '''
<button name="main" type="submit" class="mainfont">التسجيل</button>
'''
driver.get("data:text/html;charset=utf-8," + html)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='التسجيل']"))).click()
print('Done successfully clicking')
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
I tried to select an element with selenium but I'm a beginner.
Here is the element that I tried to select :
<button type="submit" class="btn btn-primary btn-block btn-form">
Connexion
</button>
I tried this lines on my script :
from selenium import webdriver
driver = webdriver.Chrome(executable_path="chromedriver.exe")
driver.get("https://skysand.fr")
connexion_button = driver.find_element_by_class_name("login")
connexion_button.click()
email_input = driver.find_element_by_id("email")
email_input.send_keys("XXXX")
password_input = driver.find_element_by_id("password")
password_input.send_keys("XXXX")
connect_button = driver.find_element_by_class_name("btn-primary btn-block btn-form")
connect_button.click()
But it is not working :(
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (513, 955)
Thanks if you can help me !
(sorry for my bad English...)
In order to select element by multiple class names you should use css_selector or XPath. Also, for this element it would better to use this css locator:
button[type='submit']
So try this:
connect_button = driver.find_element_by_css_selectro("button[type='submit']")
connect_button.click()
Also, this your code needs waits. With them it will look like this:
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
wait = WebDriverWait(driver, 20)
driver = webdriver.Chrome(executable_path="chromedriver.exe")
driver.maximize_window()
driver.get("https://skysand.fr")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".login"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#email"))).send_keys("XXXX")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#password"))).send_keys("XXXX")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[type='submit']"))).click()
It would be nice if someone knows how to select the checkbox using Selenium with Python.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
no_class = browser.find_element(By.XPATH, "//div[#id='icon-literary-collections']/following-sibling::a[1]")
no_class.click
and this is the HTML Part :
<div class="a-checkbox" style="">
<label for="checkbox-non--classifiable" style="">
<input id="checkbox-non--classifiable" type="checkbox" name="" value="" nodeid="non--classifiable" style="" class="">
<i class="a-icon a-icon-checkbox"></i>
<span class="a-label a-checkbox-label" style="">Non-Classifiable</span>
</label>
</div>
I want to select the checkbox "Non-classifiable" but i couldn't do that it tried I tried as id, name, link_text but could not detect what should be used?
You can try clicking on it by using its text in the xpath and using explicit wait so that the script waits till the element is present on the page.
You can do it like:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[text()='Non-Classifiable']")))
element.click()
You need 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
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//span[#class='a-label a-checkbox-label']"))).click()
Javascript click:
checkBox=WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//span[#class='a-label a-checkbox-label']")))
driver.execute_script("arguments[0].click();", checkBox)
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
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).