Unable to select dropdown value using selenium python - python

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()

Related

How to select prompts on a dropdown menu using Selenium and Python

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:

How to get Selenium to recognise a button and click it?

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

How to select/click in a dropdown content using selenium chromewebdriver / python

My website form have updated and my script is no more working. I cannot fix it because I'm not able to find how to select in a dropdown content using selenium chrome web driver and python.
formattedstate is the value of a formatted data (read from a file)
driver.find_element_by_css_selector(f"option[title='{formattedState}']").click()
driver.find_element_by_xpath("//*[text()='{formattedState}']").click()
driver.find_element_by_css_selector(f"option[value='{formattedState}']").click()
This is the data of the dropdown content from the webform. I selected the first state in the dropdown = Alabama
<input aria-valuetext="" role="combobox" tabindex="0"
placeholder="State/Province/Region" readonly="" height="100%" size="1" autocomplete="off" value="" data-spm-anchor-id="a2g0o.placeorder.0.i14.5f0b321eC7zfLx">
<li class="next-menu-item" title="Alabama" data-spm-anchor-id="a2g0o.placeorder.0.i17.5f0b321eC7zfLx">Alabama</li>
It should select the correct state in the dropdown content
First try to click to the combobox, then wait until state option(li) element is visible and click.
In code below, I used css selector to get li by title. If you want to find element by text, use:
wait.until(ec.visibility_of_element_located((By.XPATH, f"//li[.='{state}' and #class = 'next-menu-item']"))).click()
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
state = "Alabama"
driver.find_element_by_css_selector('input[placeholder="State/Province/Region"]').click()
wait.until(ec.visibility_of_element_located((
By.CSS_SELECTOR, f"li.next-menu-item[title='{state}']"))).click()
JS SetAttribute works surprisingly well for Combobox. Try
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('//id of element').setAttribute('value', '//value to set')");
// to click
js.executeScript("arguments[0].click();", element);

Dropdown and checkboxes with Selenium (python3)

Im trying to auto fill a registration on adidas and am not sure how to select checkboxes and especially dropdowns for DOB:
driver = webdriver.Chrome()
driver.get("https://www.adidas.co.uk/on/demandware.store/Sites-adidas-GB-Site/en_GB/MyAccount-Register")
This is whats not working for the checkbox:
driver.find_element_by_id('ffCheckbox').click()
I have no idea how to complete the dropdown for DOB.
To be able to handle target <fieldset> you first need to switch to appropriate iframe:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.switch_to_frame(driver.find_element_by_xpath('//iframe[#class="sso-iframe"]'))
Then you can handle required drop-down like:
driver.find_element_by_xpath("//a[.='DD']").click() # Open drop-down
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.XPATH,'//span[#data-val="1"]'))).click() # select first day
To click on checkbox:
driver.find_element_by_xpath('.//span[#id="consentLabel"]').click()

Button"+" is not being clicked in selenium

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()

Categories

Resources