关于html:Selenium IDE Xpath与Webdriver Xpath

Selenium IDE Xpath vs Webdriver Xpath

我是Test Automation的新手。 当我通过带有目标的Firepath定位元素时:

1
 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"}"。

我尝试转义:

1
//td[@id="catProdTd_4723290"]/div/div[2]/h2

但是什么都没有。 当我使用isElementPresent方法时,它会找到元素。
为WebDriver编写Xpath时,应该使用一些特殊的方法或规则吗?
我定义了ISelenium变量,WebDriver ...单击工作,WaitForPageToLoad工作,但这找不到元素。

1
IWebElement we= driver.FindElement(By.XPath("//td[contains(@id, 'catProdTd_4723290')]/div/div[2]/h2"));

页面HTML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<td class="productItem" id="catProdTd_4723290">
     <img border="0" alt="Fork and Spoon Set" src="/_photos/store/glass-large.jpg" id="catlproduct_4723290">
     
   
    <h2 class="product-name">Fork and Spoon Set
    $17.99
   

   
   
   
     Quantity: <input type="text" value="1" name="AddToCart_Amount" class="productTextInput" id="Units_4723290">
    (N/A in-stock)
   
   
    <input type="submit" onclick="AddToCart(192951,4723290,'',4,'','',true);return false;" value="Buy Now" name="AddToCart_Submit" class="productSubmitInput">
    Add to Wishlist
   
   
    <h4>Product Information:</h4>
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.  Aenean
    commodo ligula eget dolor.  Aenean massa.  Cum sociis natoque penatibus
     
   
    <!-- End Main -->
   
    <!-- End Product Details -->
</td>

我必须补充一点,我尝试在调试期间等待

1
Manage().Timeouts().ImplicitlyWait

但是什么都没有。 这也在其他地方发生。 我使用Firefox进行测试


您正在遇到动态属性。

我对你的第一个建议。切换到CSS。

我的第二个建议是,为什么不直接亲吻,而不是深入到整个父子等级中!

因此,让我们看看您的问题。您正在尝试获取产品名称。容易..我们可以在这里使用类。

1
css=td.productItem h2.product-name

瞧,它很容易获取..除了拥有庞大的xpath选择器之外,我们已经将其简化为CSS选择器。

因此,下一个问题是,如果页面上有多个td.productItem,我们可以使用一些方法。

尝试,

1
css=td.productItem:nth-child(1) h2.productName

这将选择类别为productItem的第一个td

注意:您可能需要指定td的父项。例如css=div#container td.productItem:nth-child(1)

更多细节...

您的xpath失败的原因是因为分配给

元素的catProdTd_4723290 id是自动生成的,导致该元素不可选择。您可以通过执行starts with解决此问题。例如,使用CSS-

1
css=td[id^='catProdTd']

会选择

注意,可能会选择多个元素。


我建议使用这种方法等待:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 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;
    }

然后检查如下:

1
2
3
4
5
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解决了点击问题:

1
2
IJavaScriptExecutor executor = (IJavaScriptExecutor)chauffeur;
IWebElement webel1 = chauffeur.FindElement(By.CssSelector("#nav ul:nth-child(1) li:nth-child(2) a[href='/products']"));

而不是使用

1
webel1.Click();

这不起作用,我用过:

1
executor.ExecuteScript("arguments[0].click();", webel1);