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()
Related
I am trying to find the text of a button. here is the html layout.
<div id="fulfillment-add-to-cart-button">
<div class="fulfillment">
<div>
<div style="position:relative">
<button class="btn ban-disabled"> Sold Out </button>
</div>
</div>
</div>
</div>
Here is what I have
driver.find_elements_by_xpath("//div[#id='fulfillment-add-to-cart-button']/div/div/div/button[0]")
I keep returning a empty array. I've tried also finding the button with the button classname however that also returns an empty array.
The button is a single element, correct? So you would find by element, not elements.
Try this, to search for that specific text within that button
button_elem = driver.find_element_by_xpath("//div[#id='fulfillment-add-to-cart-button']//button")
print(button_elem.text)
If that doesn't work we may need to see more of the HTML that surrounds that section
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
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']")
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='block';");
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)')