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']
Related
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.
I am coding a bot for tinder with selenium but its not working. Here is my code.
fb_button = self.driver.find_element_by_xpath('//*[#id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button')
fb_button.click()
This code is for clicking the facebook login button. However when i run the code, it dont work healthy.I am getting an error like this.
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate e
lement: {"method":"xpath","selector":"//*[#id="content"]/div/div[1]/div/div/main/div/div[2]/div
[2]/div/div/span/div[2]/button"}
However when i try to create an object and try to call this function, it returns something.
HTML of the button:
<button type="button" class="button Lts($ls-s) Z(0) CenterAlign Mx(a) Cur(p) Tt(u) Bdrs(100px) Px(24px) Px(20px)--s Py(0) Mih(42px)--s Mih(50px)--ml button--outline Bdw(2px) Bds(s) Trsdu($fast) Bdc(#fff) C(#fff) Bdc(#fff):h C(#fff):h Bdc(#fff):f C(#fff):f Bdc(#fff):a C(#fff):a Fw($semibold) focus-button-style W(100%) Fz(4vw)--ms" draggable="false" aria-label="Facebook ile oturum aç"><span class="Pos(r) Z(1) D(ib)">Facebook ile oturum aç</span></button>
you can wait until the element can clickable and present in HTML dom.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
fb_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "your_xpath")))
fb_button.click()
you can check the wait conditions here!
You can click on the element using xpath:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Facebook ile oturum aç']"))).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
XPath doesn't work on below path code.
HTML
<span class="voter_details">
<h4 class="v_msg" id="v_msg" style="font-size: 14px;margin-bottom:25px;">Hi, you are here XXXfor in a while, kindly enter your name!</h4>
<input value="" placeholder="Enter your Name" type="text" id="v" name="v" class="form-control name_box" maxlength="20">
</span>
Error
selenium.common.exceptions.NoSuchElementException: Message: no such element:
Unable to locate element: {"method":"xpath","selector":"//*[#id='v']"}
Code
driver.find_element(driver.find_element(By.XPATH, "//*[#id='v']")).send_keys('random.choice(list)')
Use following xpath to access the input element.
driver.find_element_by_xpath("//input[#placeholder='Enter your Name'][#id='v']").send_keys('Ashwani')
OR You can use WebdriverWait to wait for the element to be clickable and send then enter the value.
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[#placeholder='Enter your Name'][#id='v']"))).send_keys('Ashwani')
To execute that you need to have following imports.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
To send a character sequence you 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.form-control.name_box#v[placeholder='Enter your Name']"))).send_keys(random.choice(list))
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='form-control name_box' and #id='v'][#placeholder='Enter your Name']"))).send_keys(random.choice(list))
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
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="uname"]"}
I got same error while I was working with Selenium.
The problem is slow internet/big website. Try adding explicit or implicit delay or you may import time module and add time.sleep before clicking/sending keys
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
I'm trying to click on this button to go to the next page, but for some reason, I'm unable to. I tried xpath, css, and class selectors as well as the data-trekkie-id attribute, but nothing I have tried worked. Any help? Code below:
<div class="step__footer" data-step-footer="">
<button name="button" type="submit" class="step__footer__continue-btn btn " data-trekkie-id="continue_to_shipping_method_button" aria-busy="false">
<span class="btn__content">
Continue to shipping method
</span>
</button>
</div>
As per the HTML to invoke click() method on the button with text as Continue to shipping method you need to induce WebDriverWait 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.step__footer__continue-btn.btn[data-trekkie-id='continue_to_shipping_method_button']>span.btn__content"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='step__footer__continue-btn btn' and #data-trekkie-id='continue_to_shipping_method_button']/span[#class='btn__content']"))).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
This should work:
driver.find_element_by_xpath("//*[contains(local-name(), 'button') and contains(#class, 'step__footer__continue-btn')]").click()