简体   繁体   中英

Can't get handle on button attribute. Can't find it from page source

I'm having some problems getting a handle on a button, and in particular, a specific button attribute. Essentially, I have two buttons on my page. The button I need to verify has has an attribute of "disabled". The problem I have is that I can't seem to get a handle on it - it's almost as if it's not there but it's clearly within the source of my web page. Here is the code for both buttons below

<button type="button" class="btn-number" data-type="yes" data-field="quant" disabled="disabled"><i class="fht-yes"></i></button>
<a href="#"> Choose flight </a>
<button type="button" class="btn-number" data-type="no" data-field="quant"><i class="fht-no"></i></button>
</div></div>    
</div>

Here are some things I have tried:

IWebElement ButtonDisabled = Setup.driver.FindElement(By.XPath("//*[@id='box']/div/div[1]/div/button[1]"));
String VerifyButtonDisabled = ButtonDisabled.GetAttribute("outerHTML");

if (VerifyButtonDisabled.Contains("disabled"))
{
    System.Diagnostics.Debug.WriteLine("pass");
}
else
{
   System.Diagnostics.Debug.WriteLine("fail");
}

When I run this, I get the following debug trace back:

Debug Trace:

<button type="button" class="btn-number" data-type="yes" data-field="quant"><i class="fht-yes""></i></button>

Notice that the "disabled" attribute isn't being returned?

I have tried also to execute JS directly

IWebElement ButtonDisabled = Setup.driver.FindElement(By.XPath("//*[@id='box']/div/div[1]/div/button[1]"));

IJavaScriptExecutor js = Setup.driver as IJavaScriptExecutor;
String GetElement = (String)js.ExecuteScript("return arguments[0].outerHTML", ButtonDisabled);
System.Diagnostics.Debug.WriteLine(GetElement);

Again, I get the same debug trace sent back:

Debug Trace:

<button type="button" class="btn-number" data-type="yes" data-field="quant"><i class="fht-yes""></i></button>

I also tried to get the entire page's outer HTML like so:

String BodyText = Setup.driver.FindElement(By.TagName("body")).GetAttribute("outerHTML");
System.Diagnostics.Debug.WriteLine(BodyText);

One other note worth pointing out is that the button is always enabled. When I use the 'locals' debug tools on the button it always has an "enabled" property so I can't use the .getAttribute("disabled") method to verify it either.

Does anyone have any ideas? :) Many thanks for some help with this

You could just modify the XPath expression to select by disabled attribute

this.FindElement(By.XPath("//*[@id='box']/div/div[1]/div/button[@disabled='disabled']"));

Should get you the button with disabled attribute set.

this.FindElement(By.XPath("//*[@id='box']/div/div[1]/div/button[not(@disabled='disabled')]"));

Should get you the button containing no disabled attribute.

Assuming of course the rest of the original XPath expression is correct...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM