簡體   English   中英

無法使用C#在Selenium Webdriver腳本中使用XPath定位元素

[英]Unable to locate an element with XPath in selenium webdriver script with C#

在我使用selenium webdriver的自動化腳本中,網頁中ID為以下的元素:

//*[@id="SalesTable_2_General_button"] 

無法檢測到這些。 我嘗試了以下方法:

FindElement(By.XPath("//*[contains(@id,'_General_button')]"));
FindElement(By.XPath("//*[@id='SalesTable_2_General_button']"));

我無法獲得任何其他獨特的屬性,例如@ name,@ title等。請幫助我。

 <button class="appBarTab-header allowFlyoutClickPropagation" id="SalesTable_12_General_button" aria-expanded="false" type="button" data-dyn-bind="&#10; id: $data.Id + '_button',&#10; keyDown: $data.keyDown,&#10; enabled: $data.Enabled,&#10; focusIn: $data.focusIn,&#10; click: $data._headerClicked,&#10; flyout: {&#10; flyout: $('.appBar-flyout', $element.parentElement),&#10; show: $data.FlyoutExpanded, &#10; at: 'manual',&#10; openOnClick: false,&#10; clickSubscriber: $data.flyoutClickSubscriber,&#10; entranceAnimation: 'appBar-growHeight',&#10; exitAnimation: $data.flyoutExitAnimation,&#10; }"> <span class="appBarTab-headerLabel allowFlyoutClickPropagation" data-dyn-bind="&#10; text: $data.Label">General</span> </button> 

在嘗試執行findelement之前,無論您與驅動程序在哪里,都需要運行print(driver.page_source) ,但是其語法與C#相同。 確保該ID實際上顯示在頁面源中,否則您將需要幾天的時間。 如果發現頁面源不包含您的元素,請嘗試超時。 不要使用WebdriverWait命令,隱式命令或顯式命令...如果您在防火牆后面,它們將無法工作。 使用thread.sleep()命令5到10秒鍾,以查看您的page_source是否更改。 如果確實發生更改,並且找到了您的ID,則只需在腳本中保留sleep命令即可,它應該能夠檢測到您的元素。

要找到文本為General的元素,可以使用以下代碼行:

IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[@class='appBarTab-header allowFlyoutClickPropagation' and starts-with(@id,'SalesTable_')]/span[@class='appBarTab-headerLabel allowFlyoutClickPropagation' and contains(.,'General')]")));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM