Selenium locate submit button - python

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

Related

Python - clicking on button with Selenium

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

How to find href through class name and click with selenium

I'm using Selenium with python to make a spider.
A part of the web page is like this:
text - <span class="sr-keyword">name</span> text
I need to find the href and click.
I've tried as below:
target = driver.find_element_by_class_name('_j_search_link')
target.click()
target is not None but it doesn't seem that it could be clicked because after target.click(), nothing happened.
Also, I've read this question: Click on hyperlink using Selenium Webdriver
But it can't help me because in my case, there is a <span class>, not just a simple text Google.
You can look for an element with class _j_search_link that contains text Wat Chedi Luang
driver.find_element_by_xpath('//a[#class="_j_search_link" and contains(., "Wat Chedi Luang")]')
driver.find_elements_by_partial_link_text("Wat Chedi Luang").click()
I think you didn't find right element. Use CSS or Xpath to target this element then click it, like:
//a[#class='_j_search_link']
or
//a[#class='_j_search_link']/span[#class='sr-keyword']

Issue Selenium (python) unable to locate element

I am trying to click in a button of a website that is in a dropdown menu. I've tried different approach (xpath, element, link text.. ) but no one of these works and I am getting as error "unable to locate element"
The html code of the button is:
<a class="yuimenuitemlabel" href="javascript:exportToCSV()"> <span class="menu-text">Export All: CSV</span> </a> </li>
I tried the below approaches without luck:
browser.find_element_by_xpath("//*[#id='csv-export-item']/a/span").click()
browser.find_element_by_link_text('Export All: CSV')
browser.find_element_by_css_selector('span.content').click()
Any idea what could I do here? Thanks a lot.
Try this xpath once
//span[contains(text(),'Export All: CSV')]
Thank You,
Murali
Try with the following xpath method call once
browser.find_element_by_xpath("//*[text()='Export All: CSV']")
Hope it helps.

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.

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