i'm trying to create python automation scripts using selenium to make appointment times, however i'm having some trouble with clicking on certain elements. to confirm an appointment the steps are going to the webpage > selecting the date > selecting the time > hitting confirm
this is what i've got so far:
driver = webdriver.Chrome()
driver.get('<url.date>') -- this works
driver.find_element(By.ID,'corresponding time id').click() -- this works
driver.find_element(By.DATA-TEST-ID,'order_summary_page-button-book').click() -- this is where i'm struggling
i've also tried doing it by the button class, using the css selector and looking for 'Reserve Now' text, and using the xpath
when i do it by xpath, i get the error NoSuchElementException
i tried adding a webdriver wait function but that didn't seem to work either.
the appointment times are really hard to get, so i'd rather not but any wait times into the code so it can execute and book as soon as slots open
here are the elements:
https://i.stack.imgur.com/S62kR.png
any help is appreciated!
Selenium doesn't have option to identify the element by DATA-TEST-ID locator.
In this case You have to use either by xpath or css selector
driver.find_element(By.XPATH,'//button[#data-test-id="order_summary_page-button-book"]').click()
OR
driver.find_element(By.CSS_SELECTOR,'button[data-test-id="order_summary_page-button-book"]').click()
Ideally you should use explicit wait to click on the element.
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//button[#data-test-id="order_summary_page-button-book"]'))).click()
you need following imports.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
Related
I just started trying to learn to write python code today. I am trying to make a web scraper to do some stuff, but i am having trouble getting selenium to click on the specific boxes/places to get to the correct page. Because it uses Ember.js, it is a Single Page Application. This throws things off a little. I found some stuff on stackoverflow about it, but i wasnt able to get it to work. I keep getting the "NoSuchElementException" error. Here is my 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
import time
PATH = r'C:\Users\dme03\Desktop\desktop\webdrivers\chromedriver_win32\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get('https://thedailyrecord.com/reader-rankings-interface/')
time.sleep(15)
mim = driver.find_element(By.XPATH, '//img[#src="//media.secondstreetapp.com/1973100?width=370&height=180&cropmode=Fill&anchor=Center"]/parent::div')
mim.click
For the XPATH, I had to select a child element and specify the parent to select the correct element. The key identifiers in the parent element kept changing, so i was unable to select just that. I believe the XPATH is correct because when I put that into the website, it showed up as the correct line.
I have also tried using the code below, as i have seen some people suggest. When using that, I get a timed out error.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//img[#src="//media.secondstreetapp.com/1973100?width=370&height=180&cropmode=Fill&anchor=Center"]/parent::div')))
Any help would be appreciated
NEW CODE for iFrames
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//iframe[#src="https://embed-882706.secondstreetapp.com/embed/ae709978-d6d1-499e-9f5e-136cc36eab1b/"]')))
mim = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//img[#src="//media.secondstreetapp.com/1973100?width=370&height=180&cropmode=Fill&anchor=Center"]/parent::div')))
mim.click
I am having issues clicking the item. To be fair, i am not sure that you are supposed to click that element to change the webpage. That is probably an issue.
Edit: nvm im just dumb that totally worked lol :))
You are getting "NoSuchElementException" error because the desired element is inside the iframe .So you have to switch to iframe at first.See the below image screenshot
So I am trying to automate my daily thing to login to my ADP website but it seems whatever function I pick from selenium is does not find the ID that I am looking for which is "id="login-form_username".
Below is the part of the Selenim Python code.
user_textbox=driver.find_element_by_id('login-form_username')
user_textbox.send_keys(user)
user_textbox.click()
user_confirm=driver.find_element_by_id("verifUseridBtn")
user_pass_textbox=driver.find_element_by_id("login-form_password")
user_signin=driver.find_element_by_id("signBtn")
user_signin.send_keys(Keys.RETURN)
Also Attached screenshot of the site and error from my IDE. Im not sure if its because for security reasons that the Website I am trying to access wont allow this kind of thing.
Thanks
Joel
Since you didn't share the page link and not the error you seeing we can only guess what is wrong with your code.
So it can be:
You are missing a wait / delay before accessing that element.
In this case something like this should resolve your issue:
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='chromedriver.exe')
wait = WebDriverWait(driver, 20)
driver.get("https://your_site_url/")
user_textbox = wait.until(EC.visibility_of_element_located((By.ID, "login-form_username")))
user_textbox.send_keys(user)
user_textbox.click()
Alternatively you can simply add a delay before locating the elements, something like this:
time.sleep(5)
user_textbox=driver.find_element_by_id('login-form_username')
user_textbox.send_keys(user)
user_textbox.click()
time.sleep(5)
user_confirm=driver.find_element_by_id("verifUseridBtn")
user_pass_textbox=driver.find_element_by_id("login-form_password")
time.sleep(5)
user_signin=driver.find_element_by_id("signBtn")
user_signin.send_keys(Keys.RETURN)
but this is not the best approach.
2) Maybe there is an iframe there.
In this case you have to switch to that iframe.
3) Maybe you didn't set web driver to large enough size - if so please do that.
4) Possibly you have to scroll that input element into the view?
I'm having trouble to select the different options of this dropdown
in the field SIZE
https://docs.google.com/forms/d/e/1FAIpQLSdSpGLXjAV_wiI2qgg3B_KYxd4_7NR-DxHGrTySaIkAWIqmBg/viewform
Someone has a good solution?
Im able to click the element with
driver.find_element_by_xpath('/html/body/div/div[2]/form/div[2]/div/div[2]/div[14]/div/div/div[2]/div/div[1]/div[1]/div[1]').click()
But I'm not able to select the different options
You need to click the list to open it, then find and click the span with the right content inside it. Since there is a bit of drawing delay, you'll probably want it to use webdriverwait to ensure the element is ready before interacting.
This works for me:
driver = webdriver.Chrome()
driver.get("https://docs.google.com/forms/d/e/1FAIpQLSdSpGLXjAV_wiI2qgg3B_KYxd4_7NR-DxHGrTySaIkAWIqmBg/viewform")
#open the size menu
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='SIZE']/following::div[#class='quantumWizMenuPaperselectOptionList'][1]"))).click()
#select the size by it's full text
sizeToSelect = "US 11"
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class,'exportSelectPopup')]/div/span[text()='"+sizeToSelect+"']"))).click()
If you've not already got them you need the following imports:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Highlight the word SIZE and right-click and click inspect. Now do it again and it will lead you to the HTML regarding that. Keep going down from the root of that very HTML till you find the part of the size you want(as you hover over the code, it will highlight the relevant part in the website). Hope this will help you enough to make the code function.
I am using selenium and python to automate web tasks. I have tried using multiple different functions to try to click on the button I need:
Reports
.find_element_by_link_text("Reports").click()
.find_element_by_id
.find_element_by_name
.find_element_by_class_name
.find_element_by_css_selector
Can't seem to make this work, any suggestions would be appreciated.
Using "find_elements_by_xpath". Right click and copy the XPATH from the browser.
Using explicit waits could potentially solve the problem. For explicit waits, you can either use the ID or XPATH. I prefer using XPATH. For getting the XPATH, right click on the element, click Inspect, right click on Reports and choose Copy, and then Copy XPATH. Now that we have the XPATH, do the following:
button_xpath = "the xpath of your element"
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, button_xpath))).click()
The above code will wait up to 10 seconds until the button element is found. If the element is not found, a TimeoutException will be given.
The following imports are necessary:
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
This is the Screenshot of my UI and HTML DOM.
I want to go to the last page one this I can do is pressing next button many times, but I don't know how many time it should be pressed, so i don't think so its a better way.
Can you suggest me better way using selenium commands.
I used this command (pressing next continuously):
driver.find_element_by_xpath('html/body/div[2]/div/section[8]/div/div/div[3]/div/div[2]/table/tfoot/tr/td/div/ul/li[5]/a').click()
You can use try/except like:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
while True:
try:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//li[not(#class="disabled")]/a[contains(text(), "Next")]'))).click()
except TimeoutException:
break
This should allow you to click Next button until last page (until button become disabled)
P.S. You should not use absolute XPath, e.g. html/body/div[2] ... /div/ul/li[5]/a as it's sensitive to changes in DOM. Use target element's attributes or its parent/child nodes to create relative XPath