[英]C# Selenium - Unable to locate element
I am trying to get the last 'Export' button using Selenium but it throws 'no such element' Exception - Unable to locate element.我正在尝试使用 Selenium 获取最后一个“导出”按钮,但它抛出“没有这样的元素”异常 - 无法定位元素。 I've added delays before trying to locate the this element so it shouldn't be caused by HTML document not ready.我在尝试定位 this 元素之前添加了延迟,因此它不应该是由 HTML 文档未准备好引起的。 I've tried finding elements by id (the id of the outer div), xPath, classes and using css selectors but no luck with any of them.我试过通过 id(外部 div 的 id)、xPath、类和使用 css 选择器来查找元素,但对它们中的任何一个都没有运气。 How should I get the 'Export' button element correctly?我应该如何正确获取“导出”按钮元素?
Here's what I've tried but none of them worked.这是我尝试过的,但没有一个奏效。
driver.FindElement(By.XPath("//*[@id='QbDP57yUX-']/button")); //Got this xpath from chrome F12 - copy xpath, but realized the id is dynamic so this doesn't work.
driver.FindElement(By.XPath("//button[@type='button'][text()='Export']");
driver.FindElement(By.CssSector[button[class='slds-button slds-button_neutral action-bar-action-ReportExportAction reportAction report-action-ReportExportAction']]);
driver.FindElement(By.ClassName("report-action-ReportExportAction");
Below is the HTML I am trying to access.下面是我试图访问的 HTML。
<div class="action-bars">
<div class="slds-m-left--xx-small">
<div
class="slds-button-group actionBarButtonGroup"
id="rKS91liTR"
role="group"
>
<div
class="slds-tooltip-trigger"
style="display: inline-block; line-height: 1"
>
<button
class="
slds-button slds-button_icon-border
action-bar-action-searchTable
reportAction
report-action-searchTable
"
type="button"
>
<svg aria-hidden="true" class="slds-button__icon" viewBox="0 0 52 52">
<path
d="M49.598 45.298l-13.4-13.3c2.7-3.8 4.1-8.6 3.4-13.7-1.2-8.6-8.2-15.4-16.9-16.2-11.8-1.2-21.8 8.8-20.6 20.7.8 8.6 7.6 15.7 16.2 16.9 5.1.7 9.9-.7 13.7-3.4l13.3 13.3c.6.6 1.5.6 2.1 0l2.1-2.1c.6-.6.6-1.6.1-2.2zm-41.6-24.4c0-7.1 5.8-12.9 12.9-12.9 7.1 0 12.9 5.8 12.9 12.9 0 7.1-5.8 12.9-12.9 12.9-7.1 0-12.9-5.7-12.9-12.9z"
fill-rule="evenodd"
></path></svg
><span class="slds-assistive-text">Search report table</span></button
><span></span>
</div>
</div>
</div>
<div class="slds-m-left--xx-small">
<div
class="slds-button-group actionBarButtonGroup"
id="vSt8t2ooSr"
role="group"
>
<button
class="
slds-button slds-button_neutral
action-bar-action-addChart
reportAction
report-action-addChart
action-bar-sprite-button
"
type="button"
>
<svg
aria-hidden="true"
class="slds-button__icon slds-button__icon_left"
viewBox="0 0 100 100"
>
<path
d="M45.2 67.5c-.7-5.7 1.1-11.5 4.9-15.8 3.8-4.3 9.3-6.7 15-6.7.8 0 1.7.1 2.5.2.9.1 1.8.3 2.7.6 1.4.4 2.7-.7 2.5-2.1-1.1-11.9-10-21.5-21.6-23.7-1.6-.3-3.2 1-3.2 2.6v22.7c0 1.5-1.2 2.7-2.7 2.7H22.7c-1.7 0-2.9 1.5-2.6 3.2 2.2 11.5 11.7 20.4 23.6 21.6 1.4.1 2.5-1.2 2.1-2.5-.3-.9-.5-1.9-.6-2.8z"
></path>
<path
d="M43.5 40.8V22.7c0-1.7-1.5-2.9-3.2-2.6-10.8 2-18.3 9.5-20.3 20.3-.3 1.6 1 3.2 2.6 3.2h18.2c1.5-.1 2.7-1.3 2.7-2.8zm36.4 22.3c-.8-6.6-6.2-12-12.9-12.9-.6-.1-1.3-.1-1.9-.1-8.9 0-16 7.7-14.9 16.9.8 6.7 6.2 12.1 12.9 12.9.6.1 1.3.1 1.9.1 8.9 0 16-7.7 14.9-16.9zm-7.6 3.1c0 .7-.5 1.2-1.2 1.2h-3.6v3.8c0 .7-.5 1.2-1.2 1.2h-2.5c-.7 0-1.2-.5-1.2-1.2v-3.7h-3.7c-.7 0-1.2-.5-1.2-1.2v-2.5c0-.7.5-1.2 1.2-1.2h3.6v-3.7c0-.7.5-1.2 1.2-1.2h2.5c.7 0 1.2.5 1.2 1.2v3.7h3.7c.7 0 1.2.5 1.2 1.2v2.4z"
></path></svg
>Add Chart
</button>
</div>
</div>
<div class="slds-m-left--xx-small">
<div
class="slds-button-group actionBarButtonGroup"
id="JCYd4LSS01"
role="group"
>
<div
class="slds-tooltip-trigger"
style="display: inline-block; line-height: 1"
>
<button
aria-pressed="false"
class="
action-bar-action-toggleFilter
reportAction
report-action-toggleFilter
slds-button slds-not-selected
slds-button_icon-border
"
type="button"
>
<svg
aria-hidden="true"
class="slds-button__icon slds-button__icon_stateful"
viewBox="0 0 52 52"
>
<path
d="M48.3 4H3.9C2.4 4 1.7 5.7 2.6 6.8L22 29.5c.6.7.9 1.7.9 2.6v14.4c0 .8.8 1.5 1.6 1.5h3c.8 0 1.4-.7 1.4-1.5V32.1c0-1 .4-1.9 1.1-2.6L49.6 6.8c.9-1.1.2-2.8-1.3-2.8z"
></path></svg
><span class="slds-assistive-text">Filters</span></button
><span></span>
</div>
</div>
</div>
<div class="slds-m-left--xx-small">
<div
class="slds-button-group actionBarButtonGroup"
id="Bui2PwoQfe"
role="group"
>
<div
class="slds-tooltip-trigger"
style="display: inline-block; line-height: 1"
>
<button
class="
slds-button slds-button_icon-border
action-bar-action-refreshReport
reportAction
report-action-refreshReport
"
type="button"
>
<svg aria-hidden="true" class="slds-button__icon" viewBox="0 0 52 52">
<path
d="M46.5 4h-3c-.8 0-1.5.7-1.5 1.5v7c0 .9-.5 1.3-1.2.7-.3-.4-.6-.7-1-1-5-5-12-7.1-19.2-5.7-2.5.5-4.9 1.5-7 2.9-6.1 4-9.6 10.5-9.7 17.5-.1 5.4 2 10.8 5.8 14.7 4 4.2 9.4 6.5 15.2 6.5 5.1 0 9.9-1.8 13.7-5 .7-.6.7-1.6.1-2.2l-2.1-2.1c-.5-.5-1.4-.6-2-.1-3.6 3-8.5 4.2-13.4 3-1.3-.3-2.6-.9-3.8-1.6C11.7 36.6 9 30 10.6 23.4c.3-1.3.9-2.6 1.6-3.8C15 14.7 19.9 12 25.1 12c4 0 7.8 1.6 10.6 4.4.5.4.9.9 1.2 1.4.3.8-.4 1.2-1.3 1.2h-7c-.8 0-1.5.7-1.5 1.5v3.1c0 .8.6 1.4 1.4 1.4h18.3c.7 0 1.3-.6 1.3-1.3V5.5C48 4.7 47.3 4 46.5 4z"
></path></svg
><span class="slds-assistive-text">Refresh</span></button
><span></span>
</div>
</div>
</div>
<div class="slds-m-left--xx-small">
<div
class="slds-button-group actionBarButtonGroup"
id="QbDP57yUX-"
role="group"
>
<button
class="
slds-button slds-button_neutral
action-bar-action-ReportExportAction
reportAction
report-action-ReportExportAction
"
type="button"
>
Export
</button>
</div>
</div>
</div>
Please check in the dev tools
(Google chrome) if we have unique entry in HTML DOM
or not.如果我们在HTML DOM
是否有唯一条目,请检查dev tools
(谷歌浏览器)。
xpath that you should check :您应该检查的 xpath :
//button[contains(@class,'ReportExportAction')]
Steps to check:检查步骤:
Press F12 in Chrome
-> go to element
section -> do a CTRL + F
-> then paste the xpath
and see, if your desired element
is getting highlighted with 1/1
matching node. Press F12 in Chrome
-> 转到element
部分 -> 执行CTRL + F
-> 然后粘贴xpath
并查看您想要的element
是否被1/1
匹配节点突出显示。
If we have 1/1 matching node, Please make sure that :如果我们有 1/1 匹配节点,请确保:
If it satisfy all the mentioned conditions, then you can try to click it like this :如果它满足所有提到的条件,那么您可以尝试像这样单击它:
IWebElement e = Driver.FindElement(By.XPath($"//button[contains(@class,'ReportExportAction')]"));
e.Click();
You should add some add/delay before performing this interaction.您应该在执行此交互之前添加一些添加/延迟。
other xpath that you can check :您可以检查的其他 xpath :
//button[contains(@class,'ReportExportAction') and contains(text(),'Export')]
there are trailing spaces, and I think that is the reason your this line of code有尾随空格,我认为这就是你这行代码的原因
driver.FindElement(By.XPath("//button[@type='button'][text()='Export']");
did not work.不工作。
The desired element is a dynamic element so you have to induce WebDriverWait for the desired ElementToBeClickable()
and you can use either of the following Locator Strategies :所需的元素是一个动态元素,因此您必须为所需的ElementToBeClickable()
引入WebDriverWait ,您可以使用以下任一定位器策略:
CssSelector
: CssSelector
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("button.action-bar-action-ReportExportAction.report-action-ReportExportAction")));
XPath
: XPath
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[contains(@class, 'report-action-ReportExportAction') and contains(., 'Export')]")));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.