How would I get selenium python to recognise this button in HTML?
I've tried this and other tags to get it
driver.find_element_by_class_name('cookie-monster').clickbutton
print('button was clicked')
This is the button snippet code
<button class="cookie-monster__cta cookie-monster__cta--primary js-cookie-monster-accept">
<span class="glyphicon glyphicon-ok"></span>
Accept All Cookies
</button>
Your locator is wrong. The class is named cookie-monster__cta, not .cookie-monster. js-cookie-monster-accept seems to be unique class name. Use it for finding your element.
Also, you should wait for elements before clicking them.
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, "js-cookie-monster-accept")))
button = driver.find_element_by_css_selector("js-cookie-monster-accept")
button.click()
If this locator is not unique, add classes one by one to both wait and find_element_by_css_selector, like this:
.js-cookie-monster-accept.cookie-monster__cta
Related
My goal is to search for an item on the microcenter store. However, I need to select a store to choose in a dropdown menu so that I can get the proper search results. I cannot figure out how to do this. For some reason driver.find_elements_by_class_name does not work to open up the dropdown.
Here is my code:
#selenium imports
from re import search
import time
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
PATH = "C:\Program Files (x86)/chromedriver.exe"
driver = webdriver.Chrome(PATH)
what_search = "card"
driver.get("https://www.microcenter.com/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-:-MicroCenter")
#searches for what_search
print(driver.title)
search = driver.find_element_by_name("Ntt")
search.send_keys(what_search)
search.send_keys(Keys.RETURN)
#this opens up the dropdown menu
store = driver.find_element_by_id("Change-Store")
store.click()
time.sleep(1)
#these are my "attempts" to open up and click on a store location option
#storeclick = driver.find_element_by_class_name("btn btn-secondary dropdown-toggle selectorHref")
#storeclick = driver.find_element_by_class_name("dropdown-item")
storeclick = driver.find_elements_by_class_name("dropdown-itemLI store_055")
print(storeclick)
storeclick.click()
print("I TRIED")
The main issue is:
storeclick = driver.find_element_by_class_name("btn btn-secondary dropdown-toggle selectorHref")
does not work to find the dropdown menu button
<li class="dropdown-itemLI store_055"><a class="dropdown-item" href="/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-%3a-MicroCenter&storeid=055">MI - Madison Heights</a></li>
Thus I cannot click on it.
I want to click on the dropdown menu, then select a store option:
Using only class name can be tricky to find if the webpage is too big. Try using xpath instead so you can use more filters. Here is an example that can find the button:
Element:
<li class="dropdown-itemLI store_055"><a class="dropdown-item" href="/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-%3a-MicroCenter&storeid=055">MI - Madison Heights</a></li>
Xpath: //li[#class="dropdown-itemLI store_055"]//a[#class="dropdown-item"]
storeclick = drive.find_elements_by_xpath('//li[#class="dropdown-itemLI store_055"]//a[#class="dropdown-item"]')
To click on the element with text as MI - Madison Heights you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Code Block:
driver.get("https://www.microcenter.com/search/search_results.aspx?N=4294966998&NTX=mode+MatchPartial&NTT=rtx+graphics+cards&NTK=all&page=1&cat=Computer-Parts-:-MicroCenter")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-light.accept"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "MI - Madison Heights"))).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:
I want to go to this site:
http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia
Then click on the "Tabella" tab
Then see the second page (out of 64) of the table
However I failed in the first part, I cannot make a code to click on "Tabella" tab
import requests
from selenium import webdriver
driver.get('http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia')
element = driver.find_element_by_id("Tabella")
element.click()
And here is the html code that I used to search:
<li id="Tabella" class="Table_img ui-state-default ui-corner-top ui-tabs-selected ui-state-active">
<span id="span_table_img" class="span_img"></span>
Tabella </li>
Thank all!
Add time sleep before accessing the table element.
time.sleep(5)
driver.find_element_by_id('Tabella').click()
To click on Tabella tab you need to induce WebDriverWait() and wait for element_to_be_clickable() and you can use following locator.
Link Text:
driver.get("http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.LINK_TEXT,"Tabella"))).click()
XPATH:
driver.get("http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Tabella']"))).click()
You need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Problem with your code
You are trying to click an element before it is available/ready to click in DOM.
Solution
You need to wait for the element to load and become clickable to perform any kind of action on the same element.
Code
driver = webdriver.Chrome()
driver.get('http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia')
def wait_for_element_to_be_clickable(element):
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, element)))
wait_for_element_to_be_clickable("Tabella")
element = driver.find_element_by_id("Tabella")
element.click()
print("executed")
def wait_for_element_to_be_clickable(element): this method will wait for any element to become clickable before proceeding. (you can increase the time based on your page loading time).
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
I need to select a dropdown value , I checked the code it doesnt contain select class. Is there any way we can select the value without select class
By this I am clicking on dropdown which then enables the dropdown values
driver.find_element_by_xpath("(//div[#class='ot-lookup__input-container'])").click()
That is the dropdown
The code for Xpath is : (**it is same for every value)
<button _ngcontent-fru-c1=""
class="slds-media slds-listbox__option slds-listbox__option_entity slds-listbox__option_has-meta tabbable-button"
role="option"
type="button"
id="listbox-option-unique-id-[object Object]">
<!----><!---->Skipped<!---->
</button>
You can. Click to open dropdown and then click on required option (in your case button). In code below I used WebDriverWait to wait until the button with text Skipped will be clickable:
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, 10)
# ...
driver.find_element_by_xpath("(//div[#class='ot-lookup__input-container'])").click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='Skipped']"))).click()
Source code:
<input type="button" value="+" id="hour_add" class="ui-button ui-widget ui-state-default ui-corner-all" role="button" aria-disabled="false">
My code:
driver.find_element_by_xpath("//input[contains(#id, 'hour_add')]").click();
This button is not clicked.
There could be multiple reasons for the problem and there is certainly not enough information to answer, but here are the possible reasons:
there are multiple elements matching the XPath expression and a wrong element is clicked
you might need to move to the element and click:
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(element).click(element).perform()
you might need to scroll into view of the element:
driver.execute_script("arguments[0].scrollIntoView();", element)
you might need to click via javascript:
driver.execute_script("arguments[0].click();", element)
you might need to wait for element to be clickable:
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
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "hour_add"))
)
element.click()
sometimes even maximizing the browser window can help:
driver.maximize_window()
When you have Id available for the element you want to click on, then just use find_element_by_id.
driver.find_element_by_id('hour_add').click()