![](/img/trans.png)
[英]Selenium WebDriver “find_element_by_xpath” on WebElement
[英]Selenium IDE Xpath vs Webdriver Xpath
我是Test Automation的新手。 当我通过带有目标的Firepath定位元素时:
xpath=(//td[contains(@id, 'catProdTd_4723290')]/div/div[2]/h2)
Firefox找到该元素并验证文本。 但是,当我尝试使用Visual Studio 2012和Selenium Web驱动程序定位此元素时,始终出现错误:“无法定位元素:{“方法”:“ xpath”,“选择器”:“ // td [包含(@ id,'catProdTd_4723290')] / div / div [2] / h2“}”。
我尝试转义:
//td[@id=\"catProdTd_4723290\"]/div/div[2]/h2
但是什么都没有。 当我使用isElementPresent方法时,它会找到元素。 为WebDriver编写Xpath时,应该使用一些特殊的方法或规则吗? 我定义了ISelenium变量,WebDriver ...单击工作,WaitForPageToLoad工作,但这找不到元素。
IWebElement we= driver.FindElement(By.XPath("//td[contains(@id, 'catProdTd_4723290')]/div/div[2]/h2"));
页面HTML:
<td class="productItem" id="catProdTd_4723290"><div class="product-details">
<div class="product-aside"> <img border="0" alt="Fork and Spoon Set" src="/_photos/store/glass-large.jpg" id="catlproduct_4723290">
</div>
<div class="product-main">
<h2 class="product-name">Fork and Spoon Set</h2>
<div class="price"><strong>$17.99</strong></div>
<hr>
<div class="attributes"></div>
<hr>
<div class="product-col-1">
<div class="qty"> Quantity: <strong><input type="text" value="1" name="AddToCart_Amount" class="productTextInput" id="Units_4723290"></strong></div>
<div class="stock">(N/A in-stock)</div>
</div>
<div class="product-col-2">
<input type="submit" onclick="AddToCart(192951,4723290,'',4,'','',true);return false;" value="Buy Now" name="AddToCart_Submit" class="productSubmitInput">
<div class="wish"><a href="/FavoriteProcess.aspx?OID=4723290&OTYPE=27" class="favoritelink">Add to Wishlist</a></div>
</div>
<div class="product-description">
<h4>Product Information:</h4>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean
commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus
</div>
</div>
<!-- End Main -->
</div>
<!-- End Product Details -->
</td>
我必须补充一点,我尝试在调试过程中等待
Manage().Timeouts().ImplicitlyWait
但是什么都没有。 这也在其他地方发生。 我使用Firefox进行测试
您正在遇到动态属性。
我对你的第一个建议。 切换到CSS 。
我的第二个建议是,为什么不只是亲吻 ,而不是陷入整个父子等级中!
因此,让我们看看您的问题。 您正在尝试获取产品名称。 容易..我们可以在这里使用类。
css=td.productItem h2.product-name
瞧 ,它很容易获取..除了拥有庞大的xpath选择器之外,我们已经将其简化为css选择器。
因此,下一个问题是,如果页面上有多个td.productItem
,则可以使用以下几种方法。
尝试,
css=td.productItem:nth-child(1) h2.productName
这将选择带有productItem
的第一个td
。
注意:您可能需要指定td的父项。例如, css=div#container td.productItem:nth-child(1)
您的xpath失败的原因是因为分配给<td>
元素的catProdTd_4723290
id是自动生成的,导致该元素不可选择。 您可以通过以做为starts with
。 例如,使用CSS-
css=td[id^='catProdTd']
会选择<td>
进行记录,但可能会选择多个元素。
我建议使用这种方法等待:
public bool boolWaitForElementIsDisplayed(By locator, int secondsToWait)
{
WebDriverWait Wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(secondsToWait));
try
{
var FoundElement = Wait.Until<Boolean>(d =>
{
try
{
return (d.FindElement(locator).Displayed && d.FindElement(locator).Enabled);
}
catch
{
return false;
}
});
}
catch (WebDriverTimeoutException)
{
return false;
}
return true;
}
然后检查如下:
IWebElement h2Element = null;
string xpath = "//td[contains(@class,'productItem')]/div/div[contains(@class,'product-main')]/h2";
if (boolWaitForElementIsDisplayed(By.XPath(xpath), 30))
h2Element = Driver.FindElement(xpath);
因此,问题在于该页面未加载。 为什么? 因为WebElement.Click()
不起作用。 为什么单击不起作用?! 我不知道。 我使用JavascriptExecutor解决了点击问题:
IJavaScriptExecutor executor = (IJavaScriptExecutor)chauffeur;
IWebElement webel1 = chauffeur.FindElement(By.CssSelector("#nav ul:nth-child(1) li:nth-child(2) a[href='/products']"));
而不是使用
webel1.Click();
这不起作用,我用过:
executor.ExecuteScript("arguments[0].click();", webel1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.