2017-06-21 46 views
0

我想單擊結果集窗口中突出顯示的記錄。我使用xpath作爲Selenium C#:無法識別具有類名稱的xpath

""//div[contains(@class, 'email-icon icon')]/label[contains(@class, 'text')]";" 

但是,腳本無法識別記錄。

注意:Div id是動態的。

元素:在屏幕截圖中標記爲藍色的記錄。使用Xpath:「// div [contains(@class,'email-icon icon')]/label [contains(@class,'text')]」;

HTML代碼:

** ** Dhanaprabhu0106

enter image description here

+0

請用更合理的代碼(不是一半字符串)將問題轉換爲文本。 –

+0

您是否檢查選擇div的第一部分的正確性? –

+0

@JeroenHeier是的。所有的div id都是動態的。我只需要檢查標記的文本「Dhanaprabhy0106」是否可用,並點擊相同。 – Dhanaprabhu

回答

0

看來webdriver的是無法達到使用您用過的XPath所需的標籤。請嘗試以下方法:

//div[starts-with(@id, 'sc')]//div[@class='sc-view sc-table-row-view sc-collection-item even hover sc-regular-size']/div/img[@class='email-icon icon']/following-sibling::label[normalize-space(text())='Dhanaprabhu0106'] 

讓我知道,它是否適用於您。

更新1:

嘗試點擊使用IJavaScriptExecutor,如下圖所示:

IJavaScriptExecutor e = (IJavaScriptExecutor)driver; 
e.ExecuteScript("arguments[0].click();", driver.FindElement(By.xpath("//div[starts-with(@id, 'sc')]//div[@class='sc-view sc-table-row-view sc-collection-item even hover sc-regular-size']/div/img[@class='email-icon icon']/following-sibling::label[normalize-space(text())='Dhanaprabhu0106']")); 

讓我知道,它是否適合你。

+0

非常感謝Mahipal.It突出記錄。將xpath更新爲「// div [starts-with(@id,'sc')] // div [@ class ='sc-view sc-table-row-view sc-collection-item even sc-regular-size ']/div/img [@ class ='email-icon icon]]/following-sibling :: label [contains(@class,'text')]「並將它傳遞給點擊事件。但是,元素不被點擊。 – Dhanaprabhu

+0

如果您手動點擊標籤,結果如何? – Mahipal

+0

對於延遲響應,我很抱歉。它將我帶到了詳細信息頁面。 – Dhanaprabhu

0

你的XPath是不正確。看到我下面的例子。

//div[contains(@class, 'has-icon')]/label[contains(@class, 'text')]" 

//img[contains(@class, 'email-icon')]/../label[contains(@class, 'text')]" 
+0

非常感謝Buaban。上面提到的xpath突出顯示整行,而不是點擊特定的元素。 – Dhanaprabhu

+0

@Dhanaprabhu這是你想要點擊的元素? – Buaban

+0

我想點擊「Dhanaprabhu0106」,你的xpath正在工作。非常感謝您的及時幫助。 – Dhanaprabhu