How to find this button in selenium webdriver python? - python

how to find this button this is code, xpath doesn't work:
<button type="submit" data-module="tooltip" data-tooltip-config="addtocart" data-tooltip-content="#tooltip_addtocart" class="btn btn-orange-normal-plp addToCartClick product-tile__add-to-cart-CTA js--add-to-cart tooltipstered">
<span class="product-tile__add-to-cart-icon icon-cart-white"></span>
</button>
tnx

the relative Xpath to the button you want is:
//button[#type='submit']

Related

Selecting button without ID in selenium Python

I'm learning with Python Selenium and I'm trying to click a button which doesn't seem to have an id. I get an error that the element could not be scrolled into view.
Could anyone help with how I click the element?
It might be useful to know that the button only appears when you mouse over a section of the page
HTML is:
<div class="btn-group" role="group" aria-label="...">
<button type="button" class="grey-bg btn btn-default btn-expand" title="Expand"></button>
<button type="button" class="grey-bg btn btn-default btn-export" title="Export"></button>
<button type="button" class="grey-bg btn btn-default btn-clone" title="Clone"></button>
<button type="button" class="grey-bg btn btn-default btn-stamp" title="Pin"></button>
<button type="button" class="grey-bg btn btn-default btn-delete" title="Delete"></button>
</div>
I've tried to select by xpath:
driver.find_element_by_xpath('//*[#title="Export"]').click()
Thanks!
As you mentioned, it appears when you mouse hover. Kindly refer to this discussion for mouse hover. And try to look for the button after then click using javascript as this doesn't need to be displayed in order to click.
exportBtn = driver.find_element_by_xpath('//*[#title="Export"]') driver.execute_script("arguments[0].click();", exportBtn)
From the error, it looks like the element hasn't been scrolled into view. In order to scroll to the element, just add this line to ur code:
element = driver.find_element_by_xpath('//*[#title="Export"]')
driver.execute_script("arguments[0].scrollIntoView();", element)
element.click()
Hope that this helps!

How to find and click the button by "onclick" with Selenium and Python?

There are 2 buttons in a page, and the difference between these 2 buttons is "onclick".
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('security_div0')">OK</button>
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('wlan1security_div0')">OK</button>
I was thinking to use xpath:
driver.find_element_by_xpath("//form[#id='update-container-id']/div/div/div/div[2]/div/div[2]/table[1]/tbody/tr[1]/td[8]/div[3]/div/div/div/div[3]/button").click()
But it responses the error as below:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <button id="YesBtn" class="btn btn-primary" type="button"> could not be scrolled into view
Does anyone can help me to click the 2nd button correctly? Thanks a lot.
try with the x-path //button[#onclick="check_security('wlan1security_div0')"]
driver.find_element_by_xpath("//button[#onclick=\"check_security('wlan1security_div0')\"]").click()
Using Action class,
button = driver.find_element_by_xpath("//button[#onclick=\"check_security('wlan1security_div0')\"]")
ActionChains(driver).move_to_element(button).click(button).perform()
using java script executor,
driver.execute_script("javascript:check_security('wlan1security_div0')")
As per the HTML you have provided, to click on the button using the onclick() event you can use the following solution:
First Element(css_selector):
driver.find_element_by_css_selector("button.btn.btn-primary#YesBtn[onclick*='security_div0']").click()
First Element(xpath):
driver.find_element_by_xpath("//button[#class='btn btn-primary' and #id='YesBtn'][#onclick=\"check_security('security_div0')\"]").click()
Second Element(css_selector):
driver.find_element_by_css_selector("button.btn.btn-primary#YesBtn[onclick*='wlan1security_div0']").click()
Second Element(xpath):
driver.find_element_by_xpath("//button[#class='btn btn-primary' and #id='YesBtn'][#onclick=\"check_security('wlan1security_div0')\"]").click()
First and foremost, you are using a really long xpath which will be difficult to maintain. You can narrow it down further.
Now, some xpaths you can try:
1) Get the second button with the id YesBtn (assuming there are only two buttons with that attribute) :
driver.find_element_by_xpath("(//button[#id= 'YesBtn'])[2]");
2) Find by the onclick attribute:
driver.find_element_by_xpath("//button[#onclick= \'check_security(\'wlan1security_div0\')\']");
button1 with HTML :
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('security_div0')">OK</button>
XPATH :
//button[text()='OK' and #onclick="check_security('security_div0')"]
button2 with HTML :
<button class="btn btn-primary" style="width: 96px;" type="button" id="YesBtn" onclick="check_security('wlan1security_div0')">OK</button>
HTML:
//button[text()='OK' and #onclick="check_security('wlan1security_div0')"]
try:
buttonVar = browser.find_element_by_css_selector(cssSel)
ActionChains(browser).move_to_element(buttonVar).click(buttonVar ).perform()
except Exception as ex:
print("button not found)
Firefox gives you several option you can use with selenium find_element or elements;
by pressing f12 to bring up the inspector,
use the "pick an item" icon in the left corner to select the item you want to view.
This will highlight the text for that item in the inspector, which you can then right click on to bring up a menu where you select "copy" where there are at least 2 find_element options, css_selecor & xpath.
I personally don't like xpath.
They are sometimes long & unruly to work with but whatever suits your style. In my case css_selector works fine.

how to click on this button using selenium python

<button id="upload-resume" type="button" class="btn btn-primary btn-lg btn-block ng-binding" ng-show="canUpload()" ng-click="submit()" ng-disabled="submitButtonDisabled" tabindex="0" aria-hidden="false" aria-disabled="false">Continue</button>
how to click on this button using selenium python
try this code :
upload_button = driver.find_element_by_id("upload-resume")
upload_button.click()
If you wanna use Xpath , then you can use this code:
upload_button = driver.find_element_by_xpath("//input[text()='Continue']")
upload_button.click()

Cannot click a radio button inside a span Selenium Python

I am trying to click into a radio button inside a span. Trying to execute:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromeDriver = webdriver.Chrome()
chromeDriver.get('https://www.flytap.com/pt-br/')
#Works Fine
chromeDriver.find_element_by_id('origin')
origin.click()
origin.clear()
origin.send_keys('(RIO) Rio De Janeiro - todos os aeroportos , Brasil')
origin.click()
#Where I got the error
milesBox = chromeDriver.find_element_by_id("booking-type-2").click()
#Also tried:
milesBox = chromeDriver.find_element_by_id("booking-type-2").send_keys(Keys.ENTER)
#And, finally:
milesBox = chromeDriver.find_element_by_id("booking-type-2").send_keys(Keys.SPACE)
Here is the error I got:
ElementNotVisibleException: Message: element not visible
HTML code:
<!-- Payment Methods START -->
<fieldset id="booking-payment-drag" class="booking-payment">
<div class="toolbar-radio-wrapper">
<legend class="ipt-label">Reservar com:</legend>
</div>
<div class="toolbar-radio-wrapper">
<div class="radio">
<span class="checked"><input type="radio" id="booking-type-1" name="booking-type" value="1" checked></span>
</div>
<label for="booking-type-1" class="ipt-label">Dinheiro</label>
</div>
<div class="toolbar-radio-wrapper js-country-not-eligible">
<div class="radio">
<span class=""><input type="radio" id="booking-type-2" name="booking-type" value="2"></span>
</div>
<label for="booking-type-2" class="ipt-label">Milhas</label>
</div>
<div class="toolbar-radio-wrapper">
<div class="radio is-disabled js-country-eligible">
<span class=""><input type="radio" id="booking-type-3" name="booking-type" value="3" disabled></span>
</div>
<label for="booking-type-3" class="ipt-label">Miles&Cash</label>
</div>
</fieldset>
As per the HTML you have shared to click on the radio button beside the <label> with text as Milhas you can use the following line of code :
chromeDriver.find_element_by_xpath("//input[#id='booking-type-2' and #name='booking-type']").click()
You can use this xpath
//div[#class='radio']//span//input[#id='booking-type-1']
//div[#class='radio']//span//input[#id='booking-type-2']
//div[#class='radio is-disabled js-country-eligible']//span//input[#id='booking-type-3']
It seems like the origin element's dropdown covers the radio button you are trying to click. Maybe you could change the order of operations though? Clicking the destination element also opens up the section where the radio button is but it doesn't seem to cover it. This is a quick example of clicking that radio button and then you could go fillout the origin input after.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.flytap.com/pt-br/')
destination = driver.find_element_by_id('destination')
destination.click()
miles_box = driver.find_element_by_id('booking-type-2')
miles_box.click()

Python and Selenium Search for Text Outside of Tags

I'm trying to make a selenium script using python that is able to search for a specific text and click on it. Below is an example of the code I'm working with. In this example I'm searching for the string "Next" and clicking on it.
<div class="btn-container text-center">
<button id="btnNext" class="btn btn-lg btn-primary btn-block">Next</button>
</div>
So far I have tried the following:
driver.find_element_by_xpath("//*[#id='btnNext']").click()
driver.find_element_by_id('btnNext').click()
driver.find_element_by_xpath("//*[contains(text(),'" + 'Next' + "')]").click()
driver.find_element_by_xpath("//button[contains(.,'Next')]").click()
But no luck so far. Any suggestions? Thank you for all the help!

Categories