c# - 元素点击被拦截并等待
问题描述
我有一个活动的文本框,上面有一个非活动的文本框。为了发送密钥,我必须单击非活动文本框,然后将文本发送到活动文本框。我一直在这样做并且它有效,但只是有时。得到一个 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 执行器根本不起作用。行动也不起作用。也许我只是没有正确地做到这一点?感谢任何可以提供一些知识的人。
解决方案
一个可见的元素是不够的。元素必须是可交互的——你可以点击的东西。只需将您的 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());
即使您需要在文本字段中键入文本,这也有效。
推荐阅读
- excel - 双击单元格时VBA设置的单元格部分粗体随机变化
- php - PHP 无法从 XML 标记中获取内部文本
- java - Spring Application 上下文在 ApplicationContextAware 类中为空
- r - openxlsx 中的换行符
- c++ - 在 Linux 上以编程方式获取准确的 CPU 缓存层次结构信息
- elasticsearch - ElasticSearch:是否可以按分数加权进行“加权平均聚合”?
- oracle - 调用 Stored Proc 时参数的数量或类型错误
- python - matplotlib 子图未显示
- reactjs - 用 Enzyme 测试 ReduxForm
- visual-studio - 使用 Visual Studio 2017 的 Wix 3.11 安装错误