Using Selenium with Python to click on a href button - python

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

Related

python selenium: clicking buttons with various html element/css attributes

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

Selenium Element not interactable - python

I wrote a simple application in python, but when trying to click a button, I get an error Element not interactable. Here is a part of the code where it happens. I did wait for the element to render.
WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, '//*[#id="root"]/div[2]/div/div/div[2]/ul/li/button')))
driver.find_element(By.XPATH, '//*[#id="root"]/div[2]/div/div/div[2]/ul/li/button').click()
The error happens on this website: https://magiceden.io/ when clicking Connect Wallet in the top right corner and then trying to click Phantom in the opening window.
Reason behind this exception:
“element not interactabe” exception may occur due to various reason.
Element is not visible
Element is present in off screen (After scroll down it will display)
Element is present behind any other element
Element is disable
For your case it's locator, you can use below locator to click on it.
XPATH
//header/nav[1]/div[2]/div[2]/div[1]/button[2]
OR
//button[contains(#xpath,'1')]
OR
(//button[contains(.,'Connect Wallet')])[1]
The Syntax should be like
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "myXpath")))
element.click();
Imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I have tested with First and third XPATH, it's working for me, let me know if this does not solve your issue.

Cant click button with selenium

I was making a webscraper with selenium and bs4 to keep some shopping items' stock in track, there is a load more button I want to click which looks like this
button
and here is the HTML code for it, the website is https://www.nvidia.com/en-us/shop/geforce/?page=1&limit=9&locale=en-us
HTML code of the button
Whenever I try to find the element with bs4 it works but it never works with
driver.find_element_by_class_name("buy-link load-more-btn")
and I need it to click the button, can someone help me out
Am I was passing too many classes at once?
find_element_by_class_name accepts only one class. You are passing multiple classes in it.
Try implementing it with one class only
driver.find_element_by_class_name("load-more-btn").click()
Sort of a combination of the other two suggestions. If I understand Selenium correctly, it basically converts any find_element_* into a css selector, so having a space in the class name messes things up if you put both. This worked for me.
# Removes the Accept Cookies Banner
driver.find_element_by_css_selector("div.green-box-btn").click()
# Performs the click you want
driver.find_element_by_css_selector("input.buy-link").click()
driver.find_element_by_class_name("buy-link load-more-btn").click()
Please add click() to the end.
The LOAD MORE element is located at the bottom of the DOM Tree so to click on it you need to induce WebDriverWait for the element_to_be_clickable() which will automatically scroll the element within the Viewport and invoke click() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.buy-link.load-more-btn[value='LOAD MORE']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='buy-link load-more-btn' and #value='LOAD MORE']"))).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

Python Selenium how to select a dropdown option in a google form

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.

How to go on last page from number of pages using selenium python?

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

Categories

Resources