简体   繁体   中英

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:

//*[@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.

 <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#. make sure that the id actually shows up in the page source, or you'll be looking for days. 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. Use the thread.sleep() command for 5-10seconds to see if your page_source changes. 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.

要找到文本为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')]")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM