我有一个可以正确验证的链接,其后是代码target="_blank"
。我不希望单击该链接,因为它会将我带到正在测试的系统之外,并且需要重新登录。我想做的就是简单地验证链接后是否显示target="_blank"
。我正在使用Selenium Webdriver和C#
// Try and find the link for eTMAs http://www.open.ac.uk/assessment/documents/New-eTMA-Interface_000.pdf
string eTMAslink = "http://www.open.ac.uk/assessment/documents/New-eTMA-Interface_000.pdf";
Console.WriteLine($"Checking for eTMAs link http://www.open.ac.uk/assessment/documents/New-eTMA-Interface_000.pdf");
IWebElement eTMAsLink = Globals.GetElementByhref(eTMAslink);
Assert.IsNotNull(eTMAsLink);
Console.WriteLine($"Found eTMAsLink link ", eTMAsLink);
// Now validate target="_blank"> how?
答案 0 :(得分:1)
IWebElement
定义了GetAttribute
方法,可用于检查链接中是否存在属性 target
,例如:
Assert.IsNotNull(eTMAsLink);
var targetValue = eTMAslink.GetAttribute("target");
Assert.AreEqual("_blank", targetValue);
答案 1 :(得分:0)
这是您处理异常的方式。您可以尝试改善断言 about:blank
的代码 using OpenQA.Selenium;
using NUnit.Framework;
public void checknewTab(IWebDriver driver)
{
try
{
/* New tab window with IJavaScriptExecutor */
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open();");
/* Switch driver to last tab [which is blank tab] */
driver.SwitchTo().Window(driver.WindowHandles.Last());
/* Verify new Tab by using Assert If it is was not about:blank -
It will throw AssertionException */
Assert.AreEqual("about:blank", driver.Url);
}
catch(AssertionException ex)
{
/* Print [Console.Writeline] that there is not a new Tab
[about:blank] */
}
catch(Exception ex)
{
// Print to see other Exception
}
}