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']")
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
I am trying to check the checkbox but i am getting following error
selenium.common.exceptions.NoSuchElementException: Message: no such
element: Unable to locate element: {"method":"xpath","selector":"//mat-
checkbox[#id='mat-checkbox-1']/label/div"}
I tried selecting using id, css-selector and xpath but keep on getting error.
reg_date_checkbox = self.browser.find_element_by_xpath("//matcheckbox[#id='mat-checkbox-1']/label/div")
reg_date_checkbox.location_once_scrolled_into_view
reg_date_checkbox.click()
The HTML of checkbox is
<div _ngcontent-fep-c23="" class="last-date my-12 ng-star-inserted" fxlayout="column" style="flex-direction: column; box-sizing: border-box; display: flex;">
<mat-checkbox _ngcontent-fep-c23="" class="mat-checkbox mat-accent ng-untouched ng-pristine ng-valid" formcontrolname="deadline_enabled" id="mat-checkbox-1">
<label class="mat-checkbox-layout" for="mat-checkbox-1-input">
<div class="mat-checkbox-inner-container">
<input class="mat-checkbox-input cdk-visually-hidden" type="checkbox" id="mat-checkbox-1-input" tabindex="0" aria-checked="false">
<div class="mat-checkbox-ripple mat-ripple" matripple="">
<div class="mat-ripple-element mat-checkbox-persistent-ripple"></div>
</div>
<div class="mat-checkbox-frame"></div>
<div class="mat-checkbox-background">
<svg xml:space="preserve" class="mat-checkbox-checkmark" focusable="false" version="1.1" viewBox="0 0 24 24">
<path class="mat-checkbox-checkmark-path" d="M4.1,12.7 9,17.6 20.3,6.3" fill="none" stroke="white"></path>
</svg>
<div class="mat-checkbox-mixedmark"></div>
</div>
</div><span class="mat-checkbox-label"><span style="display:none"> </span>The event has a registration deadline</span>
</label>
</mat-checkbox>
<!---->
You are probably trying to click on the element which is NOT of type 'checkbox'. I see there is an input with type checkbox. Please try below mentioned code
reg_date_checkbox = self.browser.find_element_by_xpath("//input[#id='mat-checkbox-1-input']")
reg_date_checkbox.click()
Additionally if you wish you perform uncheck you can look for input element's aria-checked property to be true before performing a click.
You are using this xpath //matcheckbox[#id='mat-checkbox-1']/label/div , it should be //mat-checkbox[#id='mat-checkbox-1']/label/div , there's a - in between, you are missing that.
Still you can try with this css selector :
div.mat-checkbox-inner-container input[class^='mat-checkbox-input'][id^='mat-checkbox']
There may be chances that the checkbox is in iframe, if it is the case then first switch to frame and then try to click on check box.
You could try this:
element = self.browser.find_element_by_css("input#mat-checkbox-1-input")
element.click()
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 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()
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)')