繁体   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