Selenium Webdriver Cannot find the element of the click button - python

I don't know the element used to click the button.
I tried to write like this:
driver.find_element_by_xpath('//*/input[#type="button"]').click()
Error message:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message:
element not visible
HTML:
<input type="button" name="ctl00$c3$g_6_f947_400a_aa18_59efd84584ae$ctl00$toolBarTbl$RightRptControls$ctl00$ctl00$diidIOSaveItem" value="Save" onclick="if (!PreSaveItem()) return false;if (SPClientForms.ClientFormManager.SubmitClientForm('WPQ2')) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ctl33$g_69_f947_400a_aa18_59efd84584ae$ctl00$toolBarTbl$RightRptControls$ctl00$ctl00$diidIOSaveItem", "", true, "", "", false, true))" id="ctl00_ctl33_g_696_f947_400a_aa18_59efd84584ae_ct0_toolBarTbl_RightRptControls_ctl00_ctl00_diidIOSaveItem" accesskey="O" class="ms-ButtonHeightWidth" target="_self">

Is the 'Save' word visible? if so you can try this:
driver.find_element_by_xpath("//*[contains(text(), 'Save')]").click()

Have you tried looking for the value?
driver.find_element_by_xpath('//*/input[#value="Save"]').click()
If this doesn't work it would be helpful if you could upload the HTML for the page you are testing or provide the URL.

Not sure why you are using //*/input rather using direct //input. Here is the solution.
driver.find_element_by_xpath("//input[#type='button' and #value='Save']").click()

The desired element is a dynamic element so to locate 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(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ms-ButtonHeightWidth[value='Save'][name$='SaveItem']"))).click()
Using XPATH:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='ms-ButtonHeightWidth' and #value='Save'][contains(#name, 'SaveItem')]"))).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

Related

Select DropDown value using Python Selenium

I am trying to select a dropdown value using Selenium in Python but not able to do so. The code I get from "Copy Selector" is this.
#mui-12848
The complete HTML is
<input aria-invalid="false" autocomplete="off" type="text" class="MuiInputBase-input MuiOutlinedInput-input MuiAutocomplete-input Reports-autocompleteInput-133 MuiAutocomplete-inputFocused MuiInputBase-inputAdornedEnd MuiOutlinedInput-inputAdornedEnd" aria-autocomplete="list" autocapitalize="none" spellcheck="false" value="Monthly" id="mui-12848" aria-activedescendant="mui-12848-option-1" aria-controls="mui-12848-popup">
I have tried
s1 = Select(browser.find_element_by_id("mui-12848"))
s1.select_by_visible_text('Quarterly')
which gives the following error
UnexpectedTagNameException: Message: Select only works on elements, not on
I have also tried
browser.find_element(By.XPATH("//*[#id='mui-12848'][2]")).click();
which gives the following error
TypeError: 'str' object is not callable
Any help is appreciated.
Following is the Screenshot
Your input type for that HTML element is text, it's not a Select or a dropdown. The selenium class supports Select.
This error message...
UnexpectedTagNameException: Message: Select only works on elements, not on
...implies that you tried to use Select() class which works only on <select> element where as the desired element was an <input> element.
To click on the <input> element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("input[class*='MuiInputBase-input'][id^='mui'][value='Monthly']").click()
Using xpath:
driver.find_element_by_xpath("//input[contains(#class, 'MuiInputBase-input') and starts-with(#id, 'mui')][#value='Monthly']").click()
Ideally, 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:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[class*='MuiInputBase-input'][id^='mui'][value='Monthly']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(#class, 'MuiInputBase-input') and starts-with(#id, 'mui')][#value='Monthly']"))).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
The syntax is incorrect.
It should be like driver.find_element(By.XPATH, "//*[#id='mui-12848']").click()
Moreover, you cannot include the index inside the locator. You will need to use find_elements first and then use the index on top of that: driver.find_elements(By.XPATH,"//*[#id='mui-12848']")[2].click()
Try using expected_conditions. See below. Replace browser = ..... with your code.
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
browser = .....
# ADD YOUR CODE TO GET TO THE PAGE WITH THE BUTTON
to_click = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//*[#id='mui-12848'][2]")))
to_click.click()

Type in a textbox via python: unable to locate element

I'm trying to locate an element using python selenium, and have the html below:
<input class="form-control" type="text" placeholder="University Search">
I couldn't locate where to type what I want to type.
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path=r"D:\Python\Lib\site-packages\selenium\chromedriver.exe")
driver.get('https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology')
#<input class="form-control" type="text" placeholder="University Search">
text_area = driver.find_element_by_name('University Search')
text_area.send_keys("oxford university")
You are attempting to use find_element_by_name and yet this element has no name attribute defined. You need to look for the element with the specific placeholder attribute you are interested in - you can use find_element_by_xpath for this:
text_area = driver.find_element_by_xpath("//input[#placeholder='University Search']")
Also aside: When I open my browser, I don't see an element with "University Search" in the placeholder, only a search bar with "Site Search" -- but this might be a regional and/or browser difference.
Make sure you wait for the page to load using webdriver waits,click the popup and then proceed to target the element to send keys to.
driver.get('https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology')
driver.maximize_window()
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='OK, I agree']"))).click()
text_area=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR ,"td.uni-search.uni.sorting_disabled > div > input")))
text_area.send_keys("oxford university")
Imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Element to target
<input class="form-control" type="text" placeholder="University Search">
To send a character sequence to the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("input.form-control[placeholder='University search']").send_keys("oxford university")
Using xpath:
driver.find_element_by_xpath("//input[#class='form-control' and #placeholder='University search']").send_keys("oxford university")
Ideally, to send a character sequence to 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:
driver.get("https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control[placeholder='University search']"))).send_keys("oxford university")
Using XPATH:
driver.get("https://www.topuniversities.com/university-rankings/university-subject-rankings/2020/engineering-technology")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='form-control' and #placeholder='University search']"))).send_keys("oxford university")
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:
References
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
Very close, try the XPath when all else fails:
text_area = driver.find_element_by_xpath("//*[#id='qs-rankings']/thead/tr[3]/td[2]/div/input")
You can copy the full/relative XPath to clipboard if you're inspecting the webpage's html.

Can't add text to input type="tel" inside an IFrame using Selenium Python

HTML
I'm trying to write into "Security Code"
EDIT HTML CODE
I tried to write this:
CVXPATH = '//input[#type="tel"]'
cv=driver.find_element_by_xpath(CVXPATH)
cv.send_keys("000")
But I have this error:
line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
How can I solve this?
Problem clicking the button
I tried this method that works perfectly for other buttons (different class) on the same web page but not this specific button, I also tried using div class:
xpathoo = '//button[#class="ncss-brand pt2-sm pr5-sm pb2-sm pl5-sm ncss-btn-accent continueOrderReviewBtn mod-button-width ncss-brand\
pt3-sm prl5-sm pb3-sm pt2-lg pb2-lg d-sm-b d-md-ib u-uppercase u-rounded fs14-sm"]'
driver.find_element_by_xpath(xpathoo).click()
The element is present inside an iframe you need to switch to iframe first in order to send values in input field..
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it() and following CSS selctor.
Induce WebDriverWait() and wait for element_to_be_clickable() and following XPATH
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,'iframe.credit-card-iframe-cvv')))
cv=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//input[#id="cvNumber" and #type="tel"]')))
cv.send_keys("000")
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

Unable to locate element: while clicking a button through Selenium and Python

I want to click on a button (event) using Selenium on Python and the button code is:
<input id="workbenchLst:j_id_id509" name="workbenchLst:j_id_id509" onclick="A4J.AJAX.Submit('workbenchLst',event,{'similarityGroupingId':'workbenchLst:j_id_id509','parameters':{'ajaxSingle':'workbenchLst:j_id_id509','workbenchLst:j_id_id509':'workbenchLst:j_id_id509'} ,'containerId':'j_id_id1'} );return false;" value="Add" type="button" autocomplete="off">
My code:
driver.find_element_by_id("workbenchLst:j_id_id509").click()#add
and it isn't working, the error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="workbenchLst:j_id_id509"]
Check for the iframe/frame in your page first, if there is a frame/iframe then you need to switch the frame first like below :
driver.switch_to_frame(driver.find_element_by_id("iframeid"));
You can try clicking on the element using below XPath:
element = driver.find_element_by_xpath("//input[contains(#id, 'workbenchLst') and #value='Add']");
element.click();
Or you can try using the JavaScript Executor like below :
element = driver.find_element_by_xpath("//input[contains(#id, 'workbenchLst') and #value='Add']");
driver.execute_script("arguments[0].click();", element);
Still not working then try to give some delay, import sleep from time like below :
from time import sleep
driver.switch_to_frame(driver.find_element_by_id("iframeid"));
sleep(5);
element = driver.find_element_by_xpath("(//input[contains(#id, 'workbenchLst') and #value='Add'])[2]");
element.click();
I hope it works...
The desired element is a dynamic element so to locate 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, "input[id^='workbenchLst:'][name^='workbenchLst:'][value='Add']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[starts-with(#id,'workbenchLst:') and starts-with(#name,'workbenchLst:')][#value='Add']"))).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 add to cart button as per the given html?

My Selenium Python script is unable to click add to cart button.
HTML code:
<input type="button" value="Add to cart" title="Add to cart"
class="button-2 product-box-add-to-cart-button" onclick="AjaxCart.addproducttocart_catalog
('/addproducttocart/catalog/18/1/1');return false;">
My script:
inputElement = driver.find_element_by_xpath("/html/body/div[7]/div[4]/div[2]/div[1]/div/div[2]/div[4]/div/div[3]/div/div[2]/div[3]/div[2]/input[3]")
inputElement.click()
This is the error I get:
File "C:\Users\Raghav\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <input type="button" value="Add to cart" title="Add to cart" class="button-2 product-box-add-to-cart-button" onclick="AjaxCart.addproducttocart_catalog('/addproducttocart/catalog/18/1/1');return false;"> is not clickable at point (1334, 635). Other element would receive the click: <div class="page-loader" style="opacity: 0.924946;">...</div>
As per the HTML you have shared to click on the element you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.button-2.product-box-add-to-cart-button[title='Add to cart']"))).click()
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='button-2 product-box-add-to-cart-button' and #title='Add to cart']"))).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
Try this:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[7]/div[4]/div[2]/div[1]/div/div[2]/div[4]/div/div[3]/div/div[2]/div[3]/div[2]/input[3]")))
element.click()
Your xpath is correct, It get value but it is just not able to click. For that you can use action method to click it.
You need to Replace click event with action class, which will solve this Exception
Action method to Click :
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element("Element to Click").click().perform()
But, You have used absolute xpath which is never good idea to use. You need to use Relative xpath like following or as #DebanjanB has mentioned.
Relative xpath instead of Absolute:
//input[#title='Add to cart' and #value='Add to cart']

Categories

Resources