[英]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=" id: $data.Id + '_button', keyDown: $data.keyDown, enabled: $data.Enabled, focusIn: $data.focusIn, click: $data._headerClicked, flyout: { flyout: $('.appBar-flyout', $element.parentElement), show: $data.FlyoutExpanded, at: 'manual', openOnClick: false, clickSubscriber: $data.flyoutClickSubscriber, entranceAnimation: 'appBar-growHeight', exitAnimation: $data.flyoutExitAnimation, }"> <span class="appBarTab-headerLabel allowFlyoutClickPropagation" data-dyn-bind=" 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.