我正在通过JavaScript将某个元素滚动到视图中,但是当尝试单击该元素时,引发了一个异常,该异常表明该元素无法滚动到视图中。当我查看浏览器时,它已经成功滚动到视图中。
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(WaitTimeout));
var nextPageButton = wait.Until(ExpectedConditions.ElementToBeClickable(NextPageButtonSelector));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", nextPageButton);
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].style.width = '100%';", nextPageButton);
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].style.height = '100%';", nextPageButton);
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].style.position = 'fixed';", nextPageButton);
nextPageButton.Click();
P.S。 javascript element.click()或Actions类均无效。
答案 0 :(得分:0)
通过在单击之前添加Thread.Sleep调用解决了我自己的问题。我知道不建议这样做,但这实际上是我能够找到的唯一解决方案。 :(
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(WaitTimeout));
var nextPageButton = wait.Until(ExpectedConditions.ElementToBeClickable(NextPageButtonSelector));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", nextPageButton);
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].style.width = '100%';", nextPageButton);
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].style.height = '100%';", nextPageButton);
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].style.position = 'fixed';", nextPageButton);
Thread.Sleep(500);
nextPageButton.Click();