How do I select this dropdown button in Selenium? - python

I have this html :
<div class="dropdown float-right">
<button class="btn btn-default" type="button" id="export-button" data-toggle="dropdown">
<i class="the-download-export"></i>
Export
</button>
<div class="dropdown-menu dropdown-menu-right"><div class="dropdown-item action-export" data-format="csv" data-store="com" data-reverse-type="single"><i class="text-primary fa fa-upload" aria-hidden="true"></i>Download File</div>
This is a dropdown that has a few options. When the button is clicked, it collapses the dropdown selection and the divs appear(there are more divs for various data formats which i haven't added here for simplicity).
I have tried a few things in order to click the button with the csv data format, but everything has failed. Either not finding it or getting the div only, which is not interactible.
What is the proper way of selecting and clicking this dropdown collapsible button in Python ?

That's very little to work on.
I can recommend either checking whether you can get the data another way (e.g. api call when clicking the button) or something with x-path like
button = getFromXPath(".//button[#id='export-button']")
button.click()

Related

Is there a way to link function to button in Flask?

I would like to create a button to download the data contained in the table of my HTML page. First of all I thought of saving the data in a table in my database and then create a function that can be called up when the button is clicked (I specify that I would not want to be directed to any page and therefore to remain in the same where the button is). How can I link this function to the button? I tried using onClick() but it doesn't work.
This is the HTML code
<form method="GET" align="center" >
<button onclick="download_file()" id="download-button" class="btn btn-primary btn-lg active" type="submit">Download Table</button>
</form>
Thanks to everyone

How to distinguish 2 button base on their parent/ancestor element in Selenium (PYTHON)?

<a href="/google.com">
<div> ABC </div>
<span>
<button class="btn"> Show more </button>
<span/>
</a>
<div>
<div>ABC</div>
<span>
<button class="btn"> Show more </button>
<span/>
</div>
As you guys see here, we have 2 BUTTONS and I need to click to BUTTON whose ancestor is not <a/> tag, because if I click to button whose ancestor is tag, it will redirect me to other pages. So I don't want this behaviour.
The obvious solution is I can use absolute Xpath for this, but it's not a good way because HTML DOM or css structure could be changed so it'
s not stable.
So how can I distinguish 2 button, using ancestor or something related? I mean other better ways.
Thank you guys for helping me so much!
To select a button that has no a tag ancestor can be done with the following XPath:
//button[not(ancestor::a) and(contains(.,'Show more'))]

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.

Click a button using selenium in Python

I have the below HTML code
<footer class="aui-dialog2-footer">
<div class="aui-dialog2-footer-actions">
<button class=" aui-button aui-button-primary dialog-submit" resolved="">
Add key
</button>
<button class=" aui-button aui-button-link dialog-cancel" resolved="">Cancel</button>
Here is the attached image
I want to click this button using selenium. I have written
driver.find_element_by_xpath("//button[contains(text(),'Add key')]").click()
But this is not clicking the button. I have used find_element_by_class_name but it shows it can't resolve class name. How can I click this button using selenium.
looks like its in other iFrame look if yes just switch to it

clicking a button on a password protected website in python

I'm trying to write a script that pushes a button that looks like this in HTML code:
<button type="button" class="button save create">
<img src="img/tick-circle.png" title="" alt="">
Create
</button>
I read this post: Python: clicking a button but I can't figure out how to translate it to my problem because the website I am trying to click a button on does not take me to a new page once the button is clicked. Any suggestions?

Categories