What is the meaning of the following command in selenium?
I tried to create a automatic test cases.Then following:
//button[#type='submit'] syntax I saw in selenium tool.
This is an XPath expression. It means: find a button element anywhere in the document having type attribute value that equals to submit.
Example element matching the expression:
<button type="submit">Click Me</button>
This is an xpath for finding the element. when you inspect the element you will find button as Tag. and that Tag should have values type="submit"
Related
Am curious if anyone might know which Selenium locating element method would be used to identify the below html.
I am trying to locate and 'click'
button type="submit" tabindex="3" data-ng-click="login()" class="btn btn-default" data-ng-disabled="loginForm.$invalid" data-ng-class="{ 'gray': loginForm.$invalid }">Login</button
It depends, if you know text is not gonna change, direct use text
//button[text()='Login']
or based on attributes
//button[#data-ng-click='login()']
You can combine these two like below :
//button[#data-ng-click='login()' and text()='Login']
PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
You can following xpath,
//button[#type='submit']
//button[#data-ng-click='login()']
//button[text()='login']
driver.findElement(By.xpath("//button[text()='X']")).click();
How to find sibling element text using xpath
<label>
<span>some span</span>
</label>
<label>
Second Label
</label>
//label[normalize-space(text()) = 'some span']/following-sibling::label/text()
In Python we would do something like this :
driver.find_element(By.XPATH, "//label[//text()[normalize-space() = 'some span']]/following-sibling::label").text
basically Python Selenium bindings provide a text method for web element. getText() for Java.
Now if you want to heavily dependent on XPATH then you can try below XPATH :
//span[contains(text(),'some span')]/../following-sibling::label
or probably
//span[contains(text(),'some span')]/../following-sibling::label[contains(text(),'Second Label')]
I would use:
//label[span[text()='some span']]/following-sibling::label/text()
or if you only want to find that if it is actually the first following-element:(i.e: you don't want to find that label if there is a input in between)
//label[span[text()='some span']]/following-sibling::*[1][self::label]/text()
or if you only want to find that the first following label(if there even more following label-elements):
//label[span[text()='some span']]/following-sibling::label[1]/text()
I need to detect via xpath an element (image with href) but i know a part of href only
This is the html
<img src="//user/banners/16/08/1614708.gif" alt="AAA" data-tip="BBB" currentitem="false" class="" width="468" height="60">
I know the id 123456 (part of href)
I tried this xpath that recognize element with a part of href but working in text link only
xpath=//a[contains(#href, "123456")]
How can i detect the element using a part of href only ?
I need xpath only please.
You need get /img in //a where href attribute contains() or ends-with() your id
This is XPATH that you need. At least i would use this XPATH in this situation
//a[ends-with(#href, 'your-id-here')]/img
You can use regular expresions. Something like
driver.find_element_by_xpath("//input[starts-with (#name,'Tut')]")
or as you described
driver.find_element_by_xpath("//input[contains(#name,'sel')]").
Be awere of one thing, do not use double quotes, for the string you are searching as atribute value. Use a single quote like I previously described.
I am inspecting one button element from a web page using chrome driver and selenium. And the html code for the particular button is:
<div class="label text-left text-link link-blue text-
uppercase">Financial Statement Analysis <span class="count">(2)</span>
</div>
I have tried different element options like find element by name, xpath, link text etc. But none of them unable to locate the element.
What will be the element to locate the button. ?
try Xpath :
//span[contains(#class,'count') and text() = '(2)']
You can try with this css selector :
div.label.text-left.text-link.link-blue.text-.uppercase
To locate the element with text as Financial Statement Analysis (2) you can use the following solution:
Java Solution:
WebElement elem = driver.findElement(By.xpath("//div[#class='label text-left text-link link-blue text-uppercase'][contains(.,'Financial Statement Analysis')]"));
i need to click on dat element:
<span id="act_action9" class="" onclick="openDialog('export', event)">
text
</span>
i cant click on id,because it is dynamic. After a click i am getting window with some settings.
You should use xpath in order to click this element, so you could add an expression that unequivocally identify this element.
Could you please include more HTML code, because just with the code included is not enough to create a xpath expression.
I recommend this tutorial to start doing good xpath expressions:
http://www.w3schools.com/xpath/
See example for dynamic xpath
By.xpath("//span[#id [contains(.,'act_action')]]")
Great xpath cheatsheets: http://extract-web-data.com/5-best-xpath-cheat-sheets-and-quick-references/
You can also use xpath like following(assuming only digit is dynamic):
".//span[contains(#id, 'act_action')]"
As ID is dynamic, you can use xpath for text which is inside span.
driver.findElement(By.xpath("//span[text()='text']").click();
Or if part of ID remain common, then you can use
//span[contains(#id,'act')]