简体   繁体   English

无法使用C#在Selenium Webdriver脚本中使用XPath定位元素

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

In my automation script using selenium webdriver, some elements in the webpage with id: 在我使用selenium webdriver的自动化脚本中,网页中ID为以下的元素:

//*[@id="SalesTable_2_General_button"] 

These are failing to be detected. 无法检测到这些。 I tried the following: 我尝试了以下方法:

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

I am unable to get any other unique property like @name, @title etc. Please help me. 我无法获得任何其他独特的属性,例如@ 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> 

wherever you are with your driver right before you try to execute your findelement run a print(driver.page_source) or however it's syntax is with C#. 在尝试执行findelement之前,无论您与驱动程序在哪里,都需要运行print(driver.page_source) ,但是其语法与C#相同。 make sure that the id actually shows up in the page source, or you'll be looking for days. 确保该ID实际上显示在页面源中,否则您将需要几天的时间。 If you find the page source DOES NOT contain your element... try timing out. 如果发现页面源不包含您的元素,请尝试超时。 Don't use a WebdriverWait command, Implicit, or Explicit command... if you're behind a firewall they won't work any way. 不要使用WebdriverWait命令,隐式命令或显式命令...如果您在防火墙后面,它们将无法工作。 Use the thread.sleep() command for 5-10seconds to see if your page_source changes. 使用thread.sleep()命令5到10秒钟,以查看您的page_source是否更改。 If it does change, and your id IS found ... just leave the sleep command in your script and it should be able to detect your element. 如果确实发生更改,并且找到了您的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