Selecting dropdown option from website using Python script for automation - python

On the website I have to select a specific time duration from the drop down menu as shown in the attached picture. I need to automate it using a Python script. I am having a script in which I need to add this one thing to make the script perfectly customize. Below is some info from the website highlighting the options.
<button id="duration" type="button" class="Blockreact__Block-sc-1xf18x6-0 Buttonreact__StyledButton-sc-glfma3-0 OKxo hJoTEY" aria-expanded="false" style="font-weight: 500; width: 100%;">
<div aria-hidden="true" class="Blockreact__Block-sc-1xf18x6-0 Flexreact__Flex-sc-1twd32i-0 cwzTQS jYqxGr">
<i value="calendar_today" size="24" class="Iconreact__Icon-sc-1gugx8q-0 irnoQt material-icons">calendar_today</i>
</div>
<div class="Overflowreact__OverflowContainer-sc-7qr9y8-0 jPSCbX" tabindex="-1">7 days</div>
</button>
The purpose over here is to change 7 days to 1 month using a Python script.

I believe you should be able to do this with selenium.

Related

Upload file using Selenium opens windows file explorer [duplicate]

I was trying to automate a WordPress post content creation using Selenium Webdriver (Python). Unfortunately, I can not upload files in the post content. I have searched for the solution but most of them used send_keys which is not applicable for WP insert media (see image below). In the insert media, there are two options:
Select files
Drag files
I need a solution which will work for cross-platform (Windows, Linux etc).
I guess there might be a way using some jQuery, JS or something else. I am not very familiar with JS so I did not understand the solutions with JS.
P.S. I am working in Python, so a Python code will be helpful.
Here is the source for the media insert frame in case you need them:
<div class="media-frame-content">
<div class="uploader-inline">
<div class="uploader-inline-content no-upload-message">
<div class="upload-ui">
<h2 class="upload-instructions drop-instructions">Drop files anywhere to upload</h2>
<p class="upload-instructions drop-instructions">or</p>
Select Files
</div>
<div class="upload-inline-status">
<div class="media-uploader-status" style="display: none;">
<h2>Uploading</h2>
<button type="button" class="button-link upload-dismiss-errors"><span class="screen-reader-text">Dismiss Errors</span>
</button>
<div class="media-progress-bar">
<div></div>
</div>
<div class="upload-details">
<span class="upload-count">
<span class="upload-index"></span> / <span class="upload-total"></span>
</span>
<span class="upload-detail-separator">–</span>
<span class="upload-filename"></span>
</div>
<div class="upload-errors"></div>
</div>
</div>
<div class="post-upload-ui">
<p class="max-upload-size">Maximum upload file size: 32 MB.</p>
</div>
</div>
</div>
</div>
Finally, I've got a solution for WP add media file upload.
Actually, I have found that when the file selection window opens upon clicking the select files button, it generates a dynamic input field. Dynamic means, the ID for the input field is unique each time. Fortunately, the first part of the input field's ID remains same. For example, the ID is like html5_1bc7564i41pq5f7m1voce561a0e5. See the HTML below:
<input id="html5_1bc7564i41pq5f7m1voce561a0e5" style="font-size: 999px; opacity: 0; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;" multiple="" accept="" type="file"/>
So, what I did is, create an XPATH using the first part of the ID (html5_) and simply used send_keys and it worked perfectly. My XPATH is like:
input_file = "//input[starts-with(#id,'html5_')]"
The selenium code is:
driver.find_element_by_xpath(input_file).send_keys(file_path)
Here, file_path is the location of the file which I need to upload.
I missed the input field at the first place because it was not visible and did not realize that it is associated with the file upload. So I record the file upload steps with selenium IDE and found the dynamic input ID.
Thanks everyone, for all the suggestions and guidance.
drag and drop is possible:
Python Selenium WebDriver drag-and-drop
you can try move mouse and click "manually":
http://selenium-python.readthedocs.io/api.html#selenium.webdriver.common.action_chains.ActionChains.move_to_element
Here is the solution for you using AutoIT.
WordPress provides a select button to Upload New Media. So let us take help of that functionality.
On clicking "Select Files", File Upload dialog box would open up.
Pass the properties of File Upload dialog box to AutoIT exe.
AutoIT will fill up the filepath area with the selected file.
Now let the WebDriver click on the "Upload" button.
Let me know if this answers your query.

Search classname in HTML string using python-3

I have a string which is basically HTMl.
<div toast-component="" class="toast-success toast ng-trigger ng-trigger-flyInOut ng-animating" style="">
<button aria-label="Close" class="toast-close-button ng-tns-c31-17 ng-star-inserted" style="">
<span class="ng-tns-c31-17" aria-hidden="true">×</span></button>
<div class="toast-title ng-star-inserted" aria-label="Success!" style=""> Success! </div>
<div aria-live="polite" role="alertdialog" class="toast-message ng-star-inserted" aria-label="Data Successfully submitted" style=""> Data Successfully submitted </div>
</div>
I want to know that if this contains toast-success class. I am very new to python. Need some help. Basically I am using robotframework for automation testing
EDIT: My scenerio is that I got this string from Robotframework. I want to know that whether this div has toast-success class or toast-error class, so that I can proceed further. Not sure whether selenium has that capabilities. I am very new to this. Learning Automation testing now.

ElementNotVisibleException: element not visible Python

I'm new to Python and Selenium and I've come across an issue when trying to click on a button via webdriver.
The HTML of the div I'm trying to click through is:
<div class="">
<form method="POST">
<input class="hide" id="accept" name="accept" type="text" value="yes" readonly="">
<a href="/" class="btn btn-red">
<div class="svg-group group icn-bg-circle" data-png-fallback="">
<svg width="8" height="8">
<use xmlns:xlink=" " xlink:href=""></use>
</svg>
</div>
Decline
</a>
<button class="btn btn-green" type="submit">
<div class="svg-group group icn-bg-circle" data-png-fallback="">
<svg width="8" height="8">
<use xmlns:xlink="" xlink:href=""></use>
</svg>
</div>
Accept
</button>
</form>
</div>
I want to use .click() on the button with class name 'class="btn btn-green"'.
I have used the following code to select the element (after following the solutions in other similar SO questions.
driver.find_element_by_css_selector(".btn-green")
It looks like webdriver can find the element but when I try to apply .click() I get:
ElementNotVisibleException: element not visible
I then did some digging into the element (thinking I could bypass by using x, y coordinates to click through) but after using .size and .location I get:
{'x': 0, 'y': 0}
{'height': 0, 'width': 0}
Any help you could give on how to get around this would be really appreciated.
Thanks
The error hints to the potential problem, selenium does find it but it is not visible so you cannot click on it. You could check for visibility with is_displayed before clicking on it. Another possibility is if there is another button with that class that is hidden, try to see if you can select a visible element in the browser console (Control+Shift+J) with $(".btn-green")
Some xpaths that might work that are more robust for your problem:
//button[#type="submit" and text()="Accept"]
//button[#type="submit" and contains(#class, "btn-green")]
To click on the button with text as Accept you can use the following line of code :
driver.find_element_by_css_selector("button.btn.btn-green[type='submit']")

how to press button with same id but in different class?

I am trying to use selenium webdriver to click on a button in a different class. The webpage is as follows:
<div class="fade tab-pane" id="mm9-tab-content" role="tabpanel">
<div class="button-submit">
<button class="btn btn-primary btn-lg btn-block post" id="download" type="button">Download</button>
</div>
</div>
<div class="fade tab-pane" id="dm3-tab-content" role="tabpanel">
<div class="button-submit">
<button class="btn btn-primary btn-lg btn-block post" id="download" type="button">Download</button>
</div>
</div>
the data is in 2 classes mm9-tab-content and dm3-tab-content. I want the system to click on the download button in the dm3-tab-content.
I tried using
driver.find_element_by_xpath('xpath = (//*#id="download")[1]').click()
to get the second instance of download but it doesn't seem to work. Any ideas?
Your XPath seem to be invalid. Try below instead
driver.find_element_by_xpath('(//*[#id="download"])[2]').click()
Note that, unlike in Python, in XPath nodes indexation starts from 1, so the second element should have index [2]
If you know the order in which the buttons appear and which one you want to use, you could find all the elements with the id="download", using find_elements_by_css_selector:
buttons = driver.find_elements_by_css_selector('#download')
Then, you can access each button in order.
To click the button under mm9-tab-content class use
driver.find_element_by_css_selector('#mm9-tab-content #download').click()
To click the button under dm3-tab-content class use
driver.find_element_by_css_selector('#dm3-tab-content #download').click()

Multi-select buttons in Selenium(Python)

As part of automation testing, I'm trying to figure out what is the best way to access a multi-select button in my company website. This button is not design in the popular way and it causes me problems using Select library. Here is how it looks like:
<div class="select-options options-overflow drop-select-link" style="position: absolute; top: 373px; left: 492px; width: 53px;">
<div class="drop-holder">
<div class="drop-list" style="height: 100px; overflow-x: hidden; overflow-y: auto;">
<ul>
<li class="jcfcalc" rel="0">
<a href="#">
<span>USD</span>
</a>
</li>
<li class="option-even jcfcalc" rel="1">
<a href="#">
<span>EUR</span>
</a>
</li>
</ul>
</div>
</div>
</div>
What you see here is a multi-select button which displayed different kind of currencies. I want to be able to choose currencies in a wise way - something like Select library. However, Select library doesn't fit to my code.
Is there a library that I'm not aware of? or would I have to do it with XPaths?
Select() couldn't be used with buttons. Any buttons! It could be used with select/option elements only!
In your case you can simply use link text to select required element (which is link, not the button) as
driver.find_element_by_link_text("EUR")
driver.find_element_by_link_text("USD")

Categories

Resources