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()
Related
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.
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()
I am writing a script for a friend to automate her workflow a little bit and I am using selenium with python.
On a website, I have a radiobutton section which looks like this :
<br/>
<input id="orgtype" name="orgtype" value="euk" onclick="setvalue('orgtype','euk'); setvalue('Dcut-noTM',Dcut_noTM()); setvalue('Dcut-TM',Dcut_TM());" checked="" type="radio"/>
Eukaryotes
<br/>
<input id="orgtype" name="orgtype" value="gram-" onclick="setvalue('orgtype','gram-'); setvalue('Dcut-noTM',Dcut_noTM()); setvalue('Dcut-TM',Dcut_TM());" type="radio"/>
Gram-negative bacteria
<br/>
<input id="orgtype" name="orgtype" value="gram+" onclick="setvalue('orgtype','gram+'); setvalue('Dcut-noTM',Dcut_noTM()); setvalue('Dcut-TM',Dcut_TM());" type="radio"/>
Gram-positive bacteria
<br/>
I am trying to select the middle option which has a value of "gram-". I was wondering if anyone could help me with ( or pointing to a very simple tutorial on how to ) finding the right element by xpath? overall I know you can write ( for example :
user = browser.find_element_by_css_selector('#email')
but I am not sure on how to go about finding elements with multiple tags.
Thank you :)
As #Stack said in the comments, you should use find_element_by_xpath. If you are unsure as to how to write an xpath, Chrome has a handy option to show the XPath of a selected element. If you open the Developer Tools (Ctrl + Shift + I) and locate the element in the source, you can right click, Copy, Copy XPath.
This is just a quick fix. You should consider learning how XPath works.
Quick Solution:
Use browser.find_element_by_xpath("//input[#value='gram-']").
Explanation:
// - select element anywhere in the document starting from the current node (the root of the document).
//input - select all input elements anywhere in the document.
[#value='gram-'] - select element where value ist set to gram-.
//input[#value='gram-'] - select every input element anywhere in the document with value="gram-".
XPath Tutorials:
XPath Tutorial on W3Schools
XPath Syntax Overview
By css selector:
#orgtype[value='gram-']
Or by xPath
//input[#value= 'gram-']
To find by text containingGram-negative:
//text()[contains(.,'Gram-negative')]/preceding-sibling::input[1]
So, currently I'm using Python 3 and the selenium webdriver with Salesforce to automate admin verifications.
I've been pretty successful (even though I'm not that proficient with programming). However, I've run into an issue... I can't seem to figure out how to find an element on the page so that I can verify the text contained within is accurately being displayed.
This is what it looks like on the user's end:
The highlighted element displays as this
But whenever I search for "GlobalHeaderCommunitySwitcher", it spits back an error that it can't find it.
So I try searching for the other elements in the block of code:
<b class="zen-selectArrow"></b>PVT GBI Internal
<b class="zen-selectArrow"></b>
"PVT GBI Internal"
I've come up empty each time by searching by:
browser.find_element_by_id("globalHeaderCommunitySwitcher")
browser.find_element_by_class_name & used "zen-trigger" and "zen-selectArrow"
browser.find_element_by_xpath("//div[#class='zen-trigger'and text()='PVT GBI Internal']")
This also results in nothing being returned..
Essentially, how do I locate the element in the screenshot via the above code and then have the script verify that the text within that element ("PVT GBI INTERNAL") is present and correct?
you can use //tag[text()="value"] or //tag[contains(attribute,‘value’)]
example : browser.find_element_by_xpath("//a[#class='zen-trigger']//*[text()='PVT GBI Internal']")
//a[#class='zen-trigger']//*[contains(text(),'PVT GBI Internal')]
//a[#class='zen-trigger']//*[contains(#class="zen-selectArrow")and
contains(text(),'PVT GBI Internal')]
Open the page using the google chrome browser
Move the mouse over the element that you want to find and right-click it
Left click Inspect (at the bottom of the selection list)
Your element will be hi-lighted in the Developers tools
Right click the hi-lighted element and select Copy
Click either copy Selector or XPath depending on your preference
Paste that into your selenium find_element_by_xpath() or find_element_by_css_selector() statement as appropriate.
Say xpath
element = browser.find_element_by_xpath("your pasted xpath")
assert element.text == 'Your expected text'
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();