【问题标题】:Clicking on a field name whose unselectable is on using Selenium单击使用 Selenium 无法选择的字段名称
【发布时间】:2016-12-09 15:14:26
【问题描述】:

我试图点击一个完全不可点击的字段。我附上屏幕截图。

页面后面的Html代码是:

<td class="x-grid3-col x-grid3-cell x-grid3-td-TRAVNAME " style="width:  234px;" tabindex="0">
<div class="x-grid3-cell-inner x-grid3-col-TRAVNAME"   unselectable="on">ARUNACHALAM/SHAN</div>
</td>

我用C#写的代码如下:

Thread.Sleep(1000);
Driver.Instance.FindElement(By.XPath("//*[@id='ext - gen13']/div/table/tbody/tr/td[3]/div")).Click();

它抛出异常说它无法找到该字段。

有人可以帮忙吗?

【问题讨论】:

  • 您是否检查过以确保您的 xpath 有效/正确?
  • 没错,因为我是直接从Inspect复制过来的
  • 好吧,也许你可以使用WebDriverWait 来代替Thread.Sleep() 等到元素绝对可见。
  • 我也试过了,我认为问题不在于 Thread.Sleep 或 WebDriverWait,而在于无法选择的字段,这样就无法找到字段名称.我在此链接sqa.stackexchange.com/questions/8371/… 中得到了答案,但我不确定如何使用它。 :(

标签: c# selenium xpath css-selectors webdriverwait


【解决方案1】:

尝试使用WebDriverWait 如下:-

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(20));
var el =    wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("td.grid3-td-TRAVNAME div.x-grid3-col-TRAVNAME")));
el.Click();

已编辑:如果不幸的是由于unselectable="on" 而无法点击,请在点击之前删除此属性属性,然后使用IJavascriptExecutor,如下所示:-

var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(20));
var el =    wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("td.grid3-td-TRAVNAME div.x-grid3-col-TRAVNAME")));

IJavaScriptExecutor js = Driver.Instance as IJavaScriptExecutor;
el = (IWebElement)js.ExecuteScript("arguments[0].removeAttribute('unselectable'); return arguments[0];", el);
el.Click();

已编辑:- cssSelector 在这里不起作用尝试使用By.Xpath(),如下所示:-

var el =    wait.Until(ExpectedConditions.ElementIsVisible(By.Xpath(".//div[contains(text(), 'ARUNACHALAM/SHAN')]")));

【讨论】:

  • 嗨,Saurabh,谢谢你的回答,但我已经用过了,问题是这里的元素不可点击,如果你在这里看到 html 代码 unselectable="on"。
  • @Adrija 然后需要在单击之前删除此属性..:)
  • 我也尝试使用您编辑的代码,它没有工作.. 抛出一个异常,说 20 秒后超时,我也尝试了线程睡眠(1000),它给了我一个异常说找不到元素:(
  • @Adrija 一旦尝试使用ExpectedConditions.ElementIsExists 而不是ExpectedConditions.ElementIsVisible 并让我知道...:)
  • @Adrija 并确保此元素不在任何框架或 iframe 内...:)
【解决方案2】:

不可选择的属性

unSelectable 属性设置选择过程是否可以从元素的内容开始。如果一个元素的 unSelectable 属性设置为on,则该元素只有在选择开始于元素内容之外时才可选择。

unSelectable 属性与-moz-user-select-webkit-user-select 样式属性的区别在于-moz-user-select-webkit-user-select 样式属性指定是否可以选择元素,而unSelectable 属性仅指定是否选择过程可以从元素的内容开始,也可以不开始。另一个区别是unSelectable 属性不是继承的,而-moz-user-select-webkit-user-select 样式属性是继承的。这意味着必须在所有不可选择元素上设置unSelectable 属性,无论是否在不可选择元素的父元素上设置了unSelectable 属性。


这个用例

相关的 HTML 将有助于构建规范的答案。但是,如果该元素是动态元素或网站是基于Kendo UI,则单击该元素需要诱导WebDriverWaitelementToBeClickable(),您可以使用以下Locator Strategies 之一:

  • 使用 WebDriverWaitCssSelector

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("td.x-grid3-td-TRAVNAME > div.x-grid3-col-TRAVNAME"))).Click();
    
  • 使用 WebDriverWaitXPath

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//td[@class='x-grid3-col x-grid3-cell x-grid3-td-TRAVNAME ']/div[text()='ARUNACHALAM/SHAN']"))).Click();
    
  • 使用 IJavaScriptExecutorCssSelector

    ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CssSelector, "td.x-grid3-td-TRAVNAME > div.x-grid3-col-TRAVNAME"))));
    
  • 使用 IJavaScriptExecutor 和 CssSelector:

    ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[@class='x-grid3-col x-grid3-cell x-grid3-td-TRAVNAME ']/div[text()='ARUNACHALAM/SHAN']"))));
    

参考

您可以在以下位置找到相关的详细讨论:

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    相关资源
    最近更新 更多