[英]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.