Python - clicking on button with Selenium - python

This is the button I'm trying to click:
<button class="stkv-c-button stkv-us-button-color--background
stkv-u-background-color-transition-150
stkv-us-button-color--border stkv-us-button-color
stkv-us-button-color--fill stkv-c-button
stkv-c-button--raised stkv-c-button--big">
<span class="stkv-c-button__label stkv-c-button__label--big">Vote Now</span>
</button>
This is the xpath (pulled from Firefox):
[#id='root']/html/body/div/div/div[3]/div/div/footer/div/button"
I've tried a wide variety of ways to click on the button, all to no avail.
self.driver.find_element_by_xpath("//*[#id='root']/html/body/div/div/div[3]/div/div/footer/div/button").click()
self.driver.findElement(By.id("Vote Now")).click()
self.driver.find_element_by_name("Vote").send_keys(Keys.ENTER)
self.driver.find_element_by_id('stkv-c-button stkv-us-button-color--background stkv-u-background-color-transition-150 stkv-us-button-color--border stkv-us-button-color stkv-us-button-color--fill stkv-c-button stkv-c-button--raised stkv-c-button--big').click()
Any suggestions would be greatly appreciated!
I'm not having any luck with it.

You can find the button by display text on it.
locator = "//button[span[text()='Vote Now']]"
self.driver.find_element_by_xpath(locator).click()

Your button doesn't have an id or name, so the find_element_by_name and find_element_by_id methods will not work. find_element_by_class_name seems like the logical choice, but unfortunately it only expects only 1 class, and since your button has multiple classes, it won't work either. I would try locating the button with its CSS selector:
self.driver.find_element_by_css_selector('button.stkv-c-button.stkv-us-button-color--background.stkv-u-background-color-transition-150.stkv-us-button-color--border.stkv-us-button-color.stkv-us-button-color--fill.stkv-c-button.stkv-c-button--raised.stkv-c-button--big').click()
As long as there isn't another button on the page with the exact same CSS selector, this should give you the correct button. In general, if you want to find an element that has multiple classes, e.g. <button class="A B C">MyButton</button>, then you could do it with:
self.driver.find_element_by_css_selector('button.A.B.C')

Use this XPath : //button[normalize-space()='Vote Now']

As per the HTML you have shared to click() on the button with text as Vote Now you can use either of the following solution:
CSS_SELECTOR:
driver.find_element_by_xpath("span.stkv-c-button__label.stkv-c-button__label--big").click()
XPATH:
driver.find_element_by_css_selector("//span[#class='stkv-c-button__label stkv-c-button__label--big' and contains(.,'Vote Now')]").click()

Related

Python Selenium - Getting TimeoutException for elements belonging to a particular section of a webpage

I've been trying to click this particular button that belongs to a toggle switch in a website.
<button _ngcontent-plj-c265="" type="button" class="glyphicon glyph-mini ng-star-inserted expand" aria-expanded="true" aria-label="Title Expand or collapse filter card" style="visibility: visible;" pbi-focus-tracker-idx="6"></button>
This exists in the "filter" section of the website and I've adopted several methods and tags to perform this operation, some of which I've put here:
1. sample1 = ui.WebDriverWait(driver, 60).until(EC.element_to_be_clickable(("xpath", '//*[#id="exploreFilterContainer"]/div[2]/div/filter[14]/div/div[1]/div[1]/button[1]'))).click()
2. sample2 = ui.WebDriverWait(driver, 60).until(EC.element_to_be_clickable(("css selector", 'button.expand'))).click()
3.sample3 = driver.find_element("xpath",'//[#id="exploreFilterContainer"]/div[2]/div/filter[14]/div/div[1]/div[1]/button[1]').click()
Method 3 gave me a NoSuchElementException. Hence, I adopted methods 1 and 2 but got a TimeoutException for both. The button has no ID, so I couldn't fetch it that way, too.
But what I noticed was that there is an attribute in the button, aria-expanded="true" which when, I'm assuming, taking a value "false" will help operate the toggle button. But the catch is it can only do that if Selenium identifies the element itself, which brings us back to square one.
I'd highly appreciate any fruitful answer for my predicament. Thanks in advance.
I was able to solve my problem by switching to the <iframe> the element was within using this:
driver.switch_to.frame(0) #switching to the iframe here
tb = driver.find_element("xpath", '//*[#id="exploreFilterContainer"]/div[2]/div/filter[15]/div/div[1]/div[1]/button[1]')
driver.execute_script("arguments[0].click();", tb) #using JS to click the button
and it's advised that after all the operations pertaining to the element is performed that we switch back to normal as:
driver.switch_to.default_content()
Thanks to #Prophet #simpleApp for your efforts in trying to help me out :)

How to pull a changing class name with Python and Selenium?

I'm testing my web scraping skills with Python and Selenium and I found an button with a changing "id" "ember" and the numbers change everytime. Everything else is the same as all buttons. The only thing that's unqiue is
<button aria-label="View only People results" id="ember697" class="search-vertical-filter__filter-item-button artdeco-button artdeco-button--muted artdeco-button--2 artdeco-button--tertiary ember-view" type="button"><!---->
<span class="artdeco-button__text">
People
</span></button>
I've tried all the methods so far (i.e., id, CSS_selector, xpath, etc.).
[![Linked In button][1]][1]
Here's the error I keep getting no matter what I select.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
So, since you want to search through aria-label, you can use an XPATH:
driver.find_elements_by_xpath("//button[#aria-label='View only People results']")
EDIT: You could also use a css selector like this:
driver.find_elements_by_css_selector("button[aria-label='View only People results'']")
Try this:
option1 - i am assuming ember will be there and only numbers are changing
driver.find_elements_by_xpath("//*[starts-with(#id, 'ember')]")
option 2-
driver.find_elements_by_xpath("//*[contains(#id, 'ember')")
option 3-
driver.find_elements_by_xpath("//*[contains(#class, 'search-vertical-filter__filter-item-button')")
Hope it helps you.

find submit button in selenium without id

I have a button
<input type="submit" class="button button_main" style="margin-left: 1.5rem;" value="something">
I cannot find it by id or name and need to submit a form.
I tried doing this:
Alternatively, WebDriver has the convenience method “submit” on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and then calls submit on that. If the element isn’t in a form, then the NoSuchElementException will be raised:
element.submit()
http://selenium-python.readthedocs.org/navigating.html
But that cannot find the submit selector either.
ideas?
There are many options here, to name a few:
If class alone is unique, you can use
driver.find_element_by_css_selector(".button_main").click()
If class + value combo is unique, you can use:
driver.find_element_by_css_selector(".button_main[value='something']").click()
You can also use xpath:
driver.find_element_by_xpath("//input[#type='submit' and #value='something']").click()
If none of those work (i.e. they are not identifying button uniquely), look at the elements above the button (for example <form) and provide the xpath in format:
driver.find_element_by_xpath("//unique_parent//input[#type="submit" and #value='something']").click()
i recommend xpath chrome extension, with it you will be able to get the path by running the extension and shift-clicking on the element you want this chrome extension https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl
You could try to find the element with an XPath expression or a CSS selector like input[type="button"], and then just click the element.

Selenium locate submit button

I am working on a selenium script in Python, where I am on this stage trying to locate a submit button.
HTML
<div class="submit-buttons">
<button class="submit" type="submit">Filter</button>
</div>
I've tried and this has not worked.
So I am out of solutions:
browser.find_element_by_partial_link_text('Filter').click()
browser.find_element_by_link_text('Filter').click()
browser.find_element_by_class_name('submit').click()
Try xpath solution:
driver.find_element_by_xpath('//div[#class="submit-buttons"]/button[#class="submit"]')
If it is still not identifying, the element might be inside a frame, and you have to switch to that frame before finding the element.
By link text or by partial link text locators are going to work with links only - a elements. Here you have a button. If you want to use the button text, use the following "by xpath" locator:
//button[. = "Filter"]
This code should work :
driver.find_element_by_xpath('//button[text()='Filter']')
driver.findElement(By.cssSelector("button[type=\"submit\"]")).click();

Python - Issues with selenium button click using XPath

I'm using the following code to click a button on a page but the XPath keeps changing so the code keeps breaking:
mydriver.find_element_by_xpath("html/body/div[2]/div[3]/div[1]/div/div[2]/div[2]/div[4]/div/form[2]/span/span/input").click()
Is there a better way I should be doing this? Here is the code for the button I am trying to click:
<input class="a-button-input" type="submit" title="Button 2" name="submit.button2-click.x" value="Button 2 Click"/>
XPath is really intelligent. You could do a much more simple search for that:
mydriver.find_element_by_xpath("//input[#name='submit.button2-click.x']")
which tells: search all input elements whose name equals to 'submit.button2-click.x' which will be the element of your choice.
Don't forget to try Firefix XPath Checker add-on before going to code.
I'd use findelement(by.name(" submit.button2-click.x")).click() or use find element(by.cssSelector("selector ")).click()

Categories

Resources