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:
Related
I'm having trouble performing a click operation on a website. I'm getting a error message NoSuchElementException, but I'm not sure why because I got the class name from the site.
What am I missing?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
s = Service('C:/Program Files (x86)/chromedriver.exe')
chromeOptions = Options()
chromeOptions.headless = False
driver = webdriver.Chrome(service=s, options=chromeOptions)
list_data = []
def initialize_browser():
driver.get("https://virtualracingschool.appspot.com/#/Home")
print("starting_Driver")
click_button = driver.find_element(By.CLASS_NAME, "white-text")
driver.implicitly_wait(15)
click_button.click()
initialize_browser()
Site & Code:
I tried referencing some documents from the selenium site and it mentions for a format
<p class="content">Site content goes here.</p>`
write the code:
content = driver.find_element(By.CLASS_NAME, 'content')`
I felt like I did this properly but my site has
<a class="white-text" style="" ...>
<span>Login</span>
</a>
format. Is the <a> and "style" element hindering my code?
Use the below XPath expression:
//span[text()='Login']
Your code should look like this:
click_button = driver.find_element(By.XPATH, "//span[text()='Login']")
Below is the inspect element by this XPath for your reference:
As per your code trials:
click_button = driver.find_element(By.CLASS_NAME, "white-text")
By.CLASS_NAME, "white-text" identifies four elements within the HTML DOM:
Hence you see the see the error.
Solution
To click on the clickable 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:
driver.get('https://virtualracingschool.appspot.com/#/Home')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.white-text > span"))).click()
Using XPATH:
driver.get('https://virtualracingschool.appspot.com/#/Home')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='white-text']//span"))).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 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'm developing test automation and I want to click on element located in toolbar,I'm using python and selenium. Here is a code of website
<dl class="ToolbarDropdown_menu_2wY9X ToolbarDropdown_isHidden_3UaGr" tabindex="0" style=""
xpath="1"><dt><span class="ToolbarDropdown_title_1NBVn">New</span>
<span class="DropdownArrow_arrowIcon_dDzph DropdownArrow_down_3dlvo"></span>
</dt>
<dd class="ToolbarDropdown_item_nP-_M" style="">Dataset</dd>
<dd class="ToolbarDropdown_item_nP-_M">Project</dd>
</dl>
I need to click on element with text Dataset.
I locate element in this way
DATASET_BUTTON = (By.XPATH, '//dd[contains(text(),"Dataset")]')
And I want to perform action like that
self.driver.find_element(*VideosPageLocator.DATASET_BUTTON).click()
And there is no action but in terminal looks like my step has passed. Do you have any idea how to click on element in dropdown?
Maybe the action is done before DOM has loaded completely.
Try using WebDriverWait.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
web_driver_wait = WebDriverWait(self.driver, 10)
element = web_driver_wait.until(EC.element_to_be_clickable((By.XPATH,'//dd[contains(text(),"Dataset")]')))
element.click()
To invoke click() on the element with text as Dataset you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//dl[starts-with(#class, 'ToolbarDropdown')]//dd[starts-with(#class, 'ToolbarDropdown_item') and text()='Dataset']"))).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 am trying to scrape a table with multiple pages. The next page is obtained by clicking on the 'Next Page button' (See code snippet).
<a class="botons" id="btn2" href="javascript:void(0)">
Next Page
<i class="fas fa-long-arrow-alt-right"></i>
</a>
Selenium finds the "button" and has no trouble "clicking" via the following code:
btn_next = self.browser.find_element_by_partial_link_text("Next Page")
btn_next.click()
However, the page just refreshes and the table doesn't update to its next page.
Any clues to what's going wrong here?
Edit: table can be found at https://www.proxy-list.download/HTTPS
Edit2:
chrome_options = Options()
chrome_options.add_argument("--enable-javascript")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--headless")
There is one id assigned to that button btn2 and it's unique too.
You should give preference to id over link Text.
That said, Next Page link isn't present in view point.For that first you have to move the focus of driver like this :
wait = WebDriverWait(self.browser,10)
next_page = wait.until(EC.visibility_of_element_located((By.ID, "btn2")))
ActionChains(self.browser).move_to_element(next_page).perform()
next_page.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
The desired element is an JavaScript enabled element so to locate and click() on the element you have to induce WebDriverWait for the element_to_be_clickable() 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, "a.botons[id^='btn2'] i.fas.fa-long-arrow-alt-right"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='botons' and normalize-space()='Next Page']/i[#class='fas fa-long-arrow-alt-right']"))).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 am using below python code using selenium. click is not working on anchor tag having href = "#"
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("E:\chromedriver.exe")
driver.get('file:///E:/Selenium/validateTest.html')
driver.find_element_by_xpath("//a[#id='validateData']/i[text()=' Validate Data']").click()
Here is the web html code that I am using.
<h1>Anchor tag</h1>
Show content
<i class="fa fa-binoculars" aria-hidden="true"></i> Validate Data
As per the HTML you have shared it seems the AUT is based on JavaScript, so to click on the link with text as Validate Data you have to induce WebDriverWait for the element to be clickable and you can use either of the following options :
LINK_TEXT :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Validate Data"))).click()
CSS_SELECTOR :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-red#validateData"))).click()
XPATH :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn-red' and #id='validateData']"))).click()
Note : You will require 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
Try using a javascript executor to click your element.
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement elementToClick = driver.find_element_by_xpath("//a[#id='validateData']/i[text()=' Validate Data']");
js.executeScript("arguments[0].click();", elementToClick);
Above code needs to be adapted to python ( which i'm not familiar with, but you get the idea)
Hope this helps