how to use selenium to click 'checkout' button? - python

The html is as followed:
enter image description here
I have tried multiple methods like driver.find_element_by_css_selector and others. None worked.please help

Without knowing more information, try to use XPATH and .click()
driver.find_element_by_xpath('//a[#class="button checkout"]').click()

Related

Click a Button with Python Using Selenium

I have tried all the grabbing methods and I was not able to click on Not Now.
Xpath never worked, CSS selector..etc
enter image description here
You can try to click by text in the button, something like this:
driver.find_element_by_xpath("//button[contains(string(.),'Not Now']").click()

How do you locate and click on this button? Using python and selenium to web automate

enter image description here
I'm having trouble clicking the Sign Up Now button which seems to be different than an input type button. It is also using ng-click and not sure how I can click on the button after multiple failed attempts.
Thank you for the help!
Try copiing the XPath and then using driver.find_element_by_xpath(copied xpath) function for locating the element and then check this out - may help with clicking.
You can select it by name as JD2775 said, if you want to read a little more about it https://selenium-python.readthedocs.io/locating-elements.html
you can try with
btn = driver.find_element_by_name('btnSignUp').click()
or
btn = driver.find_element_by_name('btnSignUp')
btn.click()
or something like that

How to click an image given an Xpath in Selinum with Python?

So I have the HTML code:
enter image description here
How do I click this button? What is the Xpath for this one?
The xPath to that particular image is
/html/body/img
To click it
driver.findElement(By.xpath("/html/body/img")).click()
For xpath you need to have a path from top level upto this button.
Would look something like html\body\div\button [ just an example ].
You will have to check the whole page to navigate to that button using xpath.
Another better way could be using find element by class="auth-button positive" and then click() it

How to click on button using several conditions

I try to press a button using selenium. How do I click on a particular button using several conditions? (https://ibb.co/cJZxD7b) describing image
I have already tried something like this //span[text()=1.01], but what if webpage have few buttons with the same text.
wait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='45.00 7.75 1.04'] and //span[text()=1.04"))).click()
I expect to click on particular button
You can try with xpath
//span[#class='stn-val']
But it would probably be the best if you shared more of the HTML code because now I'm only guessing based on what I see.
Also, are you getting any error messages?
UPDATE:
Based on HTML posted in comments, use this selector:
//div[#class="st-col-bet-container"][2]/ul/li[3]/span
For tips on xpath selectors, see here.

Selenium Python: Census ACS Data- unable to select Download button in window

I am attempting to scrape the Census website for ACS data. I have scripted the whole processes using Selenium except the very last click. I am using Python. I need to click a download button that is in a window that pops when the data is zipped and ready, but I can't seem to identify this button. It also seems that the button might change names based on when it was last run, for example, yui-gen2, yui-gen3, etc so I am thinking I might need to account for this someone. Although I normally only see yui-gen2.
Also, the tag seems to be in a "span" which might be adding to my difficulty honing in on the button I need to click.
Please help if you can shed any light on this for me.
code snippet:
#Refine search results to get tables
driver.find_element_by_id("prodautocomplete").send_keys("S0101")
time.sleep(2)
driver.find_element_by_id("prodsubmit").click()
driver.implicitly_wait(100)
time.sleep(2)
driver.find_element_by_id("check_all_btn_above").click()
driver.implicitly_wait(100)
time.sleep(2)
driver.find_element_by_id("dnld_btn_above").click()
driver.implicitly_wait(100)
driver.find_element_by_id("yui-gen0-button").click()
time.sleep(10)
driver.implicitly_wait(100)
driver.find_element_by_id("yui-gen2-button").click()
enter image description here
enter image description here
Instead of using the element id, which as you pointed out varies, you can use XPath as Nogoseke mentioned or CSS Selector. Be careful to not make the XPath/selector too specific or reliant on changing values, in this case the element id. Rather than using the id in XPath, try expressing the XPath in terms of the DOM structure (tags):
//*/div/div/div/span/span/span/button[contains(text(),'Download')]
TIL you can validate your XPath by using the search function, rather than by running it in Selenium. I right-clicked the webpage, "inspect element", ctrl+f, and typed in the above XPath to validate that it is the Download button.
For posterity, if the above XPath is too specific, i.e. it is reliant on too many levels of the DOM structure, you can do something shorter, like
//*button[contains(text(),'Download')]
although, this may not be specific enough and may require an additional field, since there may be multiple buttons on the page with the 'Download' text.
Given the HTML you provided, you should be able to use
driver.find_element_by_id("yui-gen2-button")
I know you said you tried it but you didn't say if it works at all or what error message you are getting. If that never works, you likely have an IFRAME that you need to switch to.
If it works sometimes but not consistently due to changing ID, you can use something like
driver.find_element_by_xpath("//button[.='Download']")
On the code inspection view on Chrome you can right click on the item you want to find and copy the xpath. You can they find your element by xpath on Selenium.

Categories