I got a button named "Photos" that I want to find through Selenium:
<label class="Label" id="item--label">Photos</label>
I tried to find it through XPath but I got the code below (which repeats in more buttons in the page):
//*[#id="item--label"]
Is there a way to locate the item by label?
I have tried:
driver.find_element_by_tag_name('Photos')
But no success.
If anyone could help, I would be glad!
Thanks!
The most complete XPath expression for this element is
//label[#class="label" and(#id = "item--label") and(text()="Photos")]
So to find it with Selenium you can use this command:
button = driver.find_element_by_xpath('//label[#class="label" and(#id = "item--label") and(text()="Photos")]')
But you definitely do not need to mention all these attributes
driver.find_element_by_xpath(".//label[text()='Photos']")
Try this xpath:
//*[#id="item--label" and text()="Photos"]
Related
I dont know how to click a button in selenium without the id can anyone help me? I have already tried the class but it wont work.
There is no id. You need to use other locator. For example css selector.
driver.find_element_by_css_selector(
".btn.btn-secondary.btn-lg.btn-block")
Also, wait for this element to be clickable before clicking it.
Check my other answers
you can use any attribute , as there is an data attribute use that :
Xpath:
//button[#data-track="Sign In"]
CSS:
button[data-track="Sign In"]
You can also use :
//button[contains(text(),"Sign In")]
sytax for xpath or css is :
xpath:
//tagname[#attribute="value"]
css:
tagname[attribute="value"]
so you can check for any attribute and its value. not only id
I wanted to get full X-path of element instance using selenium python ?
Input:
element_inst = driver.find_element_by_link_text('Next')
I wanted to get full X-path of element_inst
Sample Output:
/html/body/div[1]/div[6]/div[1]/div[1]/div[1]/content-viewer/div/div/div/div[2]/div[2]/div/activity-viewer/div/div/phase-map-directive/div/div/div/ul/li[3]/div/span[1]
is there any way to print full X-path of element?
Option -1
I found below link which may help you:
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/5520
Option 2-
You can use Sikuli and record code for below steps and use clipboard data as xpath in your code
Right Click and click on inspect
On selected element just right click
Copy-> Xpath
Hope this helps
I have a website that I'm trying to automate but I can't find a particular link with selenium to click on. It looks like a link on the website, but when I use the chrome "inspect" function, it looks like it might be a button (???). I've tried copying the Xpath, but that doesn't work.
Here is the HTML behind the link
<button ng-bind-html="::ListingCtrl.copy.planListing.noPreference" track="No Preference" ng-click="::ListingCtrl.enterNoPreference()" class="link ally-focus-within">No Preference</button>
The Xpath for it is
//*[#id="mainContent"]/div/div/div/div[2]/ul/li[1]/h2/button
The text of the link is "No Preference", so also tried the following
elem_NoPreference = browser.find_element_by_xpath('//#track=\'No Preference\'')
But I'm not sure if my quote escape characters are correct.
I also unsuccessfully tried the following
elem_NoPreference = browser.find_element_by_link_text('No Preference') <br>
elem_NoPreference = browser.find_element_by_class_name('link ally-focus-within') <br>
elem_NoPreference = browser .find_element_by_css_selector("button[class='link ally-focus-within']
I should mention that the following are unique in the HTML. So, if there is a way to find these using the Xpath, it would be helpful
ListingCtrl.copy.planListing.noPreference
ListingCtrl.enterNoPreference()
track = "No Preference"
I'm at my wits end here. Any help would be appreciated.
Thank you!
If you don't plan to do any I18N / L10N testing you can stick to No Preference text, in this case the selector would be:
//button[text()='No Preference']
other option is basically the same, but instead of text it looks for track HTML attribute:
More information:
XPath Tutorial
XPath Operators & Functions
Xpath cheatsheet
Try with this xpath:
elem_NoPreference = browser.find_element_by_xpath("//button[#track='No Preference']")
or, if you want to select the element by the containing text:
elem_NoPreference = browser.find_element_by_xpath("//button[contains(.,'No Preference')]")
I have the following code when I inspect on Chrome.
<span id="button-1111-btnInnerEl" class="x-btn-inner x-btn-inner-center" unselectable="on" style="">New Email</span>
I need to click on the label "New Email", but how should invoke it in Selenium (I'm using Python).
def CreateMail():
EmailButton="//*[contains(text(),'New Email')]"
driver.find_elements_by_xpath(EmailButton) // there is no method to enable click.
You can use execute_script
driver.execute_script("document.getElementById('button-1111-btnInnerEl').click()")
driver.find_element_by_id("button-1111-btnInnerEl").click()
Thanks all for your help. Finally i found the answer to my question.I had to add a wait statement, before finding the key. key wasn't present when the page loads, so had to wait a little bit to find the correct key.
def CreateMail():
try:
element = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID, "button-1143-btnInnerEl")))
driver.find_element_by_id("button-1143-btnInnerEl").click()
except TimeoutException:
print ("Loading took too much time!")
Hope This XPath will work for you.If you want to validate xpath using your chrome browser just paste this text on your chrome console $x("//*[text()='New Email']") and check how many elements found using this XPath
driver.find_elements_by_xpath("//span[text()='New Email']")
Your statement : "driver.find_elements_by_xpath(EmailButton)" . Click does not work on group of elements. It is actionable only on single element. So you use a singular finder.
driver.find_**element**_by_id(EmailButton).click()
As per the HTML you have shared, the id attribute looks dynamic to me. So we must construct a dynamic xpath or css. Additionally instead of find_elements we have to use find_element so a single WebElement is returned and we can invoke the click() method. Finally, if you look at the node properly, the unselectable attribute is on so we will take help of JavascriptExecutor as follows :
myElement = driver.find_element_by_xpath("//span[starts-with(#id, 'button-')][#class='x-btn-inner x-btn-inner-center']")
driver.execute_script("arguments[0].click();", myElement);
Actually there is different way for locating elements
but in your case there is a ID,
so you can prefer id if its exists
find solution below :
driver.find_element_by_id("button-1111-btnInnerEl").click()
I have following html code:
I need to hook the "edit" icon for particular element which has title "test".
I tried to do
//*[contains(text(), 'test')]/following-sibling::div/span[#title='Edit']
But it doesn't work.
I can't identify this element by just title = Edit, because there are a lot of elements which will have this button edit. The only unique this is first title "test" and following sibling "Edit".
Any ideas? Would appreciate any help.Thank you
You can select the div with title="test", then get the span with title="Edit" like this:
xpath("//div[#title='test']/following-sibling::div/span[#title='Edit']")
or directly select the div with span title='Edit':
xpath("//div/span[#title='Edit']")
Was able to locate this element with following path:
//*[contains(., 'test')]/following-sibling::div/span[#title='Edit']
Thank you everyone for your help.