<div id="ColumnMask">
<div id="MiddleColumnPositioner">
<div id="LeftColumnPositioner">
<div id="MiddleColumnWrapper">
<div id="MiddleColumn">
<div class="bdrBottom">
<div class="pad">
<h2>Account Information</h2>
<br/>
<div class="padLeft padRight">
</div>
</div>
<!-- MiddleColumn -->
</div>
<!-- MiddleColumnWrapper -->
Hi. I'm trying to locate tag and surprisingly nothing works. Here are my examples:
public IWebElement AccountInfoTitle()
{
return Driver.FindElement(By.XPath("//*[@id='MiddleColumn']/div[2]/h2[contains(.,'Account Information')]"));
}
public IWebElement AccountInfoTitle()
{
return Driver.FindElement(By.XPath("//h2 [contains(.,'Account Information')]"));
}
Do you have any idea why? It looks correct to me.
But If I'm moving the quotes (putting them like this ['@id=MiddleColumn']) it works perfect, but I'm assuming that in this case Selenium kind of confused and not even validate it, therefore it's passing the test. Here is the example:
public IWebElement AccountInfoTitle()
{
return Driver.FindElement(By.XPath("//*['@id=MiddleColumn']/div['h2=Account Information']"));
}
Thanks in advance.
Not a complete answer, but part of it: the XPath
//*[@id='MiddleColumn']/div[2]/h2[contains(.,'Account Information')]
doesn't return anything for your input as this is a misunderstanding - div[2]
selects the second direct child div
of the element with @id='MiddleColumn'
which does not exist.
One correct XPath for your input is
//*[@id='MiddleColumn']/div/div/h2[contains(.,'Account Information')]
For illustration - the XPath //*[@id='MiddleColumn']/div[2]/h2
works in case your input would look like this (relevant part):
<div id="MiddleColumn">
<div class="bdrBottom>Example</div> //div[1]
<div class="pad"> //div[2]
<h2>Account Information</h2>
For the second XPath that should work
//h2[contains(.,'Account Information')]
an XPath expression tester returned the correct result, but I'm unsure if the space that you have after h2
- //h2 [contains(.,'Account Information')]
- could cause Selenium to fail.
You might want to change the AccountInfoTitle method into:
public IWebElement AccountInfoTitle()
{
return Driver.FindElement(By.XPath("//h2[contains(text(),'Account Information')]"));
}
Try this xpath:
//div[@class='pad']/h2[.='Account Information']
The above will locate the 'h2' element with exact innerHTML/text as 'Account Information' under a 'div' element with class attribute as 'pad' .
i think the best practices to locate an element contain String as the following:
-retrun all element using class name or id
in java we can use FindElements or FindElement i'm not sure but i think the same in c#
public IWebElements getMyElement(){
return Driver.FindElements(By.cssSelector(".pad");
}
and you can find your element by using for loop :
public IWebElement getElementContainString(String expectedString){
for(int i=0; i<getMyElement().size(); i++){
IWebElement element = getMyElement().get(i).getText().toString();
if(element.contain(expectedString){
return getMyElement().get(i);
breack;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.