Selecting checkout button using selenium python did not work - python

For the following html, my code could not select the checkout button, what's wrong going with it?
My code:
browser.find_element_by_id('checkout').click()
browser.find_element_by_css_selector('input[value="Proceed To Checkout"]')
Html:
<div id="itemBasketButtons" class="clearfix">
<input id="updateOrder" class="button" type="button" value="Save Changes"></input>
<input id="checkout" class="button" type="button" value="Proceed To Checkout »"></input>
</div>

value="Proceed To Checkout »"
Note that "»" sign at the end. You probably should use
browser.find_element_by_css_selector(u'input[value="Proceed To Checkout »"]')

Can you find element itemBasketButtons?
browser.find_element_by_id('itemBasketButtons')
If so, try to select it, and then the second child (the checkout button)
browser.find_element_by_css_selector('#itemBasketButtons:nth-child(2)')

Related

How to click a button type through selenium using Python

Following is the HTML code of the button I was trying to click:
<div class="login-button">
<button type="submit" class="btn login-from-btn">Login</button>
</div>
What are the possible ways that I can click that button using either XPath/Class Name/CSS Selector?
I have tried as follows:
driver.find_element_by_class_name("login-button btn login-from-btn").click()
driver.find_element_by_xpath("//button[text()='Login']")
can you try this one and let me know does it working

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()

How to send text to invisible input field

I need to automate file uploading.
Here is a HTML code for path-to-file input field and button for manual uploading:
<div class="ctrl_div">
<input id="fileupload" class="hid" name="files" accept="application/zip" data-url="/Server/file/upload" type="file">
<button id="fileBtn" class="btn btn-primary" type="button">Upload</button>
</div>
When I try
driver.find_element_by_xpath('//input[#id="fileupload"]').send_keys(path_to_file)
I get ElementNotVisibleException
I also tried
driver.execute_script("document.getElementById('fileupload').style.visibility = 'visible';")
But input field remains invisible for webdriver.
Any ideas?
P.S. Adding implicit/explicit wait will not make a trick
You should try with
driver.execute_script(document.getElementById('fileUpload').style.display='bloc‌​k';");

Python mechanize - two buttons of type 'submit'

I have a mechanize script written in python that fills out a web form and is supposed to click on the 'create' button. But there's a problem, the form has two buttons. One for 'add attached file' and one for 'create'. Both are of type 'submit', and the attach button is the first one listed. So when I select the forum and do br.submit(), it clicks on the 'attach' button instead of 'create'. Extensive Googling has yielded nothing useful for selecting a specific button in a form. Does anyone know of any methods for skipping over the first 'submit' button and clicking the second?
I tried using the nr parameter, without any luck.
I was able to get it to work with a combination of the name and label parameters, where "label" seems to correspond to the "value" in the HTML:
Here are my two submit buttons:
<input type="submit" name="Preview" value="Preview" />
<input type="submit" name="Create" value="Create New Page" />
... and here's the code that clicks the first one, goes back, and then clicks the second:
from mechanize import Browser
self.br = Browser()
self.br.open('http://foo.com/path/to/page.html')
self.br.select_form(name='my_form')
self.br['somefieldname'] = 'Foo'
submit_response = self.br.submit(name='Preview', label='Preview')
self.br.back()
self.br.select_form(name='my_form')
self.br['somefieldname'] = 'Bar'
submit_response = self.br.submit(name='Create', label='Create New Page')
There's a variant that also worked for me, where the "name" of the submit button is the same, such as:
<input type="submit" name="action" value="Preview" />
<input type="submit" name="action" value="Save" />
<input type="submit" name="action" value="Cancel" />
and
self.br.select_form(name='my_form')
submit_response = self.br.submit(name='action', label='Preview')
self.br.back()
submit_response = self.br.submit(name='action', label='Save')
IMPORTANT NOTE - I was only able to get any of this multiple-submit-button code to work after cleaning up some HTML in the rest of the page.
Specifically, I could not have <br/> - instead I had to have <br /> ... and, making even less sense, I could not have anything between the two submit buttons.
It frustrated me to no end that the mechanize/ClientForm bug I hunted for over two hours boiled down to this:
<tr><td colspan="2"><br/><input type="submit" name="Preview" value="Preview" /> <input type="submit" name="Create" value="Create New Page" /></td></tr>
(all on one line) did not work, but
<tr><td colspan="2"><br />
<input type="submit" name="Preview" value="Preview" />
<input type="submit" name="Create" value="Create New Page" /></td></tr>
worked fine (on multiple lines, which also shouldn't have mattered).
I like mechanize because it was easy to install (just copy the files into my include directory) and because it's pretty simple to use, but unless I'm missing something major, I think that bugs like this are kind of awful - I can't think of a good reason at all why the first example there should fail and the second should work.
And, incidentally, I also found another mechanize bug where a <textarea> which is contained within a <p> is not recognized as a valid control, but once you take it out of the <p> container it's recognized just fine. And I checked, textarea is allowed to be included in other block-level elements like <p>.
I would suggest you to use Twill which uses mechanize (mostly monkeypatched).
So say you have form with some fields and two submit buttons with names "submit_to_preview" and "real_submit". Following code should work.
BTW remember this is not threadsafe so you might want to use locks in case if you want to use the code in a threaded env.
import twill.commands
b = twill.get_browser()
url = "http://site/myform"
twill.commands.go(url)
twill.commands.fv("2", "name", "Me")
twill.commands.fv("2", "age", "32")
twill.commands.fv("2", "comment", "useful article")
twill.commands.browser.submit("real_submit")
Hope that helps. Cheers.
Use the 'click' method. E.g.
mybrowser.select_form(nr=0)
req = mybrowser.click(type="submit", nr=1)
mybrowser.open(req)
Should work.
I can talk from experience using HTTP, rather than mechanize, but I think this is probably what you want.
When there are two submit buttons in a form, a server can determine which one was pressed, because the client should have added an argument for the submit button. So:
<form action="blah" method="get">
<p>
<input type="submit" name="button_1" value="One" />
<input type="submit" name="button_2" value="Two" />
</p>
</form>
Will take you either the URL:
blah?button_1=One
or:
blah?button_2=Two
Depending on which button was pressed.
If you're programatically determining what arguments are going to be sent, you need to add an argument with the name of the submit button that was pressed, and it's value.

Categories

Resources