【问题标题】:Element Click Intercepted With Wait元素点击被拦截并等待
【发布时间】:2021-10-28 20:26:29
【问题描述】:

我有一个活动文本框,上面有一个非活动文本框。为了发送密钥,我必须单击非活动文本框,然后将文本发送到活动文本框。我一直在这样做并且它有效,但只是有时。得到一个 ElementIntercepted 异常是很常见的,事实上得到这个比它成功地能够点击该项目更多。下面是html。

<div class="step__field step__form__block" id="dob-field-inactive"> 
      <input name="dob-plain" class="step__input" type="text" placeholder="Date of Birth (mm / dd / yyyy)" autocomplete="off"> 
      <input name="dob-format" type="hidden" value="MDY"> 
      <span class="step__field__indicator"></span> 
</div>

<div class="step__field--date step__form__block phantom" id="dob-field-active"> 
    <input name="dob-month" class="step__input--date--mm" type="number" placeholder="mm" min="1" max="12" data-maxlength="2" value="" autocomplete="bday-month"> 
    <span class="step__input--date-separator">/</span> 
    <input name="dob-day" class="step__input--date--dd" type="number" placeholder="dd" min="1" max="31" data-maxlength="2" value="" autocomplete="bday-day"> <span class="step__input--date-separator">/</span>
     <input name="dob-year" class="step__input--date--yyyy" type="number" placeholder="yyyy" min="1900" max="2021" data-maxlength="4" value="" autocomplete="bday-year"> 
    <span class="step__field__indicator"></span> 
</div>

这是我的代码,

ChromeOptions options = new ChromeOptions();
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
options.AddArgument("--user-agent=Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25");
IWebDriver driver = new ChromeDriver(driverService, options);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
driver.Manage().Cookies.DeleteAllCookies();
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30);

System.Drawing.Size size = new System.Drawing.Size(200, 400);
driver.Manage().Window.Size = size;

driver.Navigate().GoToUrl("https://account.battle.net/creation/flow/creation-full");

//click on inactive textbox to activate the active one
wait.Until(ExpectedConditions.ElementIsVisible(By.Name("dob-plain"))).Click(); //ClickIntercepted
                
                
                //enter Month
wait.Until(ExpectedConditions.ElementIsVisible(By.Name("01"))).SendKeys(month.ToString());
                
                //enter Day
wait.Until(ExpectedConditions.ElementIsVisible(By.Name("01"))).SendKeys(day.ToString());
               
                //enter Year
wait.Until(ExpectedConditions.ElementIsVisible(By.Name("1993"))).SendKeys(year.ToString());

这是我发现效果最好的代码。使用 javascript 执行器根本不起作用。行动也不起作用。也许我只是没有正确地做到这一点?感谢任何可以提供一些知识的人。

【问题讨论】:

  • 奇怪的相同代码在我的本地工作得很好。
  • 尝试多次运行它。我运行了大约 10 个线程,大约 60% 会失败。我的一个朋友试过了,根本没用。 100% 的时间都失败了。
  • 他和我唯一的区别是他在另一个国家,但是html是完全一样的。
  • 不确定我是从印度尝试的,它确实有效,我尝试了 3-4 次
  • 这真的很奇怪。有没有其他方法可以提高成功率?

标签: c# selenium selenium-chromedriver webdriverwait


【解决方案1】:

一个可见的元素是不够的。该元素必须是可交互的——你可以点击它。只需将您的 ExpectedConditions 方法调用更改为 ExpectedConditions.ElementToBeClickable(...)

wait.Until(ExpectedConditions.ElementToBeClickable(By.Name("dob-plain")))
    .Click();
               
wait.Until(ExpectedConditions.ElementToBeClickable(By.Name("01")))
    .SendKeys(month.ToString());
                
wait.Until(ExpectedConditions.ElementToBeClickable(By.Name("01")))
    .SendKeys(day.ToString());
               
wait.Until(ExpectedConditions.ElementToBeClickable(By.Name("1993")))
    .SendKeys(year.ToString());

即使您需要在文本字段中输入文本,也可以使用。

【讨论】:

    猜你喜欢
    • 2020-02-23
    • 2021-09-04
    • 2019-11-08
    • 2022-01-05
    • 1970-01-01
    • 2020-01-04
    • 2021-10-08
    • 2019-09-24
    • 2020-08-07
    相关资源
    最近更新 更多