Grab and Click Button using Selenium Python - python

There are several size options under 'SELECT SIZE' on this site: https://www.myntra.com/sports-shoes/puma/puma-men-black-eternity-nitro-running-shoes/14521968/buy
I have been trying to somehow click one button to choose size of the shoes using python selenium but every time it says "no such element: unable to locate element:.....".Here's what I have tried till now.
size_button = self.find_element(
By.CSS_SELECTOR,
'button[class="btn default outline size-btn big selected"]'
)
size_button = self.find_element(
By.CSS_SELECTOR,
'ul[class="sizes-list list-unstyled list-inline padding-md-left padding-md-bottom"]'
)
size_button = self.find_element(
By.XPATH, '/html/body/div[2]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div/div/div[4]/div[6]/div[2]/div[1]/div[2]/ul/li[1]/button'
)
size_button = self.find_element(
By.XPATH, '//[#id="reactPageContent"]/div/div/div[4]/div[6]/div[2]/div[1]/div[2]/ul/li[1]/button'
)
Here's the error for one of the above code:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"button[class="btn default outline size-btn big selected"]"}

For click on buttons this code may help you
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.support.wait import WebDriverWait
if __name__ == '__main__':
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path="chromedriver", chrome_options=options)
driver.get('https://www.myntra.com/sports-shoes/puma/puma-men-black-eternity-nitro-running-shoes/14521968/buy')
# get all buttons by common class
buttons = driver.find_elements(By.CLASS_NAME, 'size-buttons-size-button')
# wait for first button (buttons[0]) be clickable and click
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(buttons[0])).click()
time.sleep(3)
# clicking in all buttons
for button in buttons:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(button)).click()
time.sleep(3)
driver.quit()
note1: you can use for example buttons[0].click() but the button may not loaded, giving for you a error
note2: in this example i used the class as selector but you have countless selector options (in future, with more experience, you will be able to choose the clearest selector!)

To click on Size 6 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.myntra.com/sports-shoes/puma/puma-men-black-eternity-nitro-running-shoes/14521968/buy')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='size-buttons-size-button size-buttons-size-button-default size-buttons-big-size']//p[text()='6']"))).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:

Related

How click a div with selenium in python to download a file (error: rootNode.elementsFromPoint is not a function)

I tried to download the Youtube charts Weekly from https://charts.youtube.com/charts/TopSongs/ as csv. (the download button is in the upper right corner in a SVG icon)
I used this code and tried two ways to click it but both gave me this error selenium.common.exceptions.JavascriptException: Message: javascript error: rootNode.elementsFromPoint is not a function (Session info: chrome=91.0.4472.106)"
And this is my code, I already make sure that I found the right HTML element with download_button.get_attribute("outerHTML")
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://charts.youtube.com/charts/TopSongs/')
time.sleep(4)
#######first attempt######
download_button = driver.find_element_by_xpath("//div[#class='download-container style-scope ytmc-charts']/paper-icon-button")
action = ActionChains(driver)
action.move_to_element(download_button)
action.click()
action.perform()
#######second attempt######
wait = WebDriverWait(driver, 10)
check_box_el = wait.until(ec.visibility_of_element_located((By.XPATH, "//div[#class='download-container style-scope ytmc-charts']/paper-icon-button")))
ActionChains(driver).move_to_element(check_box_el).click().perform()
driver.quit()
Any idea about it? Thanks :)
See if this works:-
download_elm = driver.find_element_by_xpath(".//*[#id='download-button']")
driver.execute_script("arguments[0].click();", download_elm)

cannot select button by xpath in selenium python

button = self.driver.find_element_by_xpath(
"/html/body/div[4]/div[2]/div/div[1]/main/div/div[2]/div/div/div/form/div[3]/button")
button.click()
when I try to run my code I get the exception: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[4]/div[2]/div/div[1]/main/div/div[2]/div/div/div/form/div[3]/button"}
(Session info: chrome=91.0.4472.164
meanwhile its the xpath of the button which is found https://www.zalando.fr/login
and this is the element
Se connecter
i've tried finding it by class name and everything it just doesn't work
Your locator is wrong.
Also use explicit wait.
Try this:
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)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[data-testid='login_button']"))).click()
you can try this where need to handle the cookie window which is getting displayed after the page is getting loaded and used explicitWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
driver.get("https://www.zalando.fr/login")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='OK']"))).click()
#here I'm passing the user name
driver.find_element_by_xpath("//*[#id='login.email']").send_keys("email")
#here passing password
driver.find_element_by_xpath("//*[#id='login.password']").send_keys("password")
#clicking on the `Se connecter` button
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#data-testid='login_button']"))).click()
The xpath ended up being wrong. I copied it from chrometools and tried later with brave and I got the correct xpath.

python selenium - Cant find the element of sign in button

I'm trying to create an automation test in Asos (for practice purpose only) however I'm having a hard time locating this sign-in element...
I need to click on that sign-in button.
these are the element I got in inspect:
a class="_1336dMe _1uUU2Co _1336dMe _1uUU2Co" href="https://my.asos.com/my-account?
lang=en-GB&store=COM&country=GB&keyStoreDataversion=3pmn72e-27"
data-testid="signin-link" tabindex="-1">Sign In
I had the same problem when trying to find this button on Google Maps. Is the sign in button on a pop up window? Then the problem is becuse you have to change between frames.
Here is a code sample:
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
driver = webdriver.Chrome('driver path')
url = 'url'
driver.get(url)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[#id="consent-bump"]/div/div[1]/iframe')))
agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
agree.click()
#back to the main page
driver.switch_to_default_content()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchboxinput"]'))).send_keys('gostilne')
search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchbox-searchbutton"]')))
search.click()
Make shure that the xpath for frames and buttons is correct.

"The provided double value is non-finite" error when attempting to get the locate an element using Selenium and Python

I am trying to get selenium to scroll down to a button in a website that has the text 'Try it out!' inside the button.
My problem is that there are no uniquely ID'd elements around the button to which I could scroll the view to. In addition, when I inspect the website with dev tools and search from the text 'Try it out!' in the HTML I get 72 results. I figured out that I need the 18th button but I am unable to get the browser to scroll to the button. Instead I get an error saying "The provided double value is non-finite".
Could you please look at the code below and give me an explanation to why I the browser is not scrolling down to the button?
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
import pathlib
# Get path to chromedriver
file_path = pathlib.Path(__file__).parent.absolute()
chromedriver_path = str(file_path)+"\\chromedriver.exe"
class Scraper:
def __init__(self):
# Open website
self.driver = webdriver.Chrome(chromedriver_path)
print(self.driver)
self.driver.get(
"https://flespi.io/gw/#/tags/!/devices/get_devices_dev_selector_messages")
sleep(5)
# Get the 18th button that says 'Try it out!'. Position()=17 because starts with 0.
element = self.driver.find_element_by_xpath(
'(//input[#value="Try it out!"])[position()=17]')
# Scroll to the button and click it
actions = ActionChains(self.driver)
actions.move_to_element(element).perform()
element.click()
sleep(5)
Scraper()
To grab the Try it out button and click on it first create a webdriver wait to wait for the element to be clickable.
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='devices_get_devices_dev_selector_messages_content']/form/div[3]/input")))
element.click()
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Please try the below code.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)
driver.get('https://flespi.io/gw/#/tags/!/devices/get_devices_dev_selector_messages')
Try_btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#devices_get_devices_dev_selector_messages_content > form > div.sandbox_header > input')))
action.move_to_element(Try_btn).click().perform()
This code works for me.
This error message...
Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite
...implies that the WebDriver instance was unable to find the element for one or the other reasons:
The element haven't loaded properly when you tried to interact with it.
Element is within an <iframe> / <frame>
The style attribute of the element contains display: none;
Element is within an shadow DOM
You can find a relevant detailed discussion in javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite
This use-case
Among the 72 elements with text as Try it out! i.e.
<input class="submit" type="submit" value="Try it out!" data-sw-translate="">
barring the desired element all the other 71 elements have an ancestor with style attribute set as display: none; overflow: hidden;. Where as only the desired element is having style attribute set as overflow: hidden;.
to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#devices_get_devices_dev_selector_messages_content input[value='Try it out!']"))).click()
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='devices_get_devices_dev_selector_messages_content']//input[#value='Try it out!']"))).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

How to click the button on the following website?

I am new to the world of python and I am trying to select a couple of options on the following website and then click the search button to update results. However, I cannot get the button to respond.
I tried using search button.click() and .submit() and I have tried to implicitly wait. I have also used the code below to wait until the button is clickable. When executing the code, it highlights the button but doesn't seem to release the click; almost like a half click.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
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.Safari()
driver.get('https://leasing.com/personal/car-leasing/')
element = driver.find_element_by_id('selUpfront')
select = Select(element)
select.select_by_value("3")
element = driver.find_element_by_id('selMileage')
select = Select(element)
select.select_by_value("8000")
searchbutton = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "search-button")))
searchbutton.click()
I would expect the search results to be updated with the conditions above.
Seems you were close. To click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#search-button>i.fa.fa-search#search-button-icon"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='search-button']/i[#class='fa fa-search' and #id='search-button-icon']"))).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:
There are 2 elements with the search-button you need to use the xpath to locate specific
searchbutton = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='search-button']")))
searchbutton.click()

Categories

Resources