Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 硒';元素无法滚动到视图中';使用JavaScript将元素滚动到视图中之后_C#_Selenium - Fatal编程技术网

C# 硒';元素无法滚动到视图中';使用JavaScript将元素滚动到视图中之后

C# 硒';元素无法滚动到视图中';使用JavaScript将元素滚动到视图中之后,c#,selenium,C#,Selenium,我正在通过JavaScript将一个元素滚动到视图中,但当尝试单击该元素时,出现了一个异常,表示该元素无法滚动到视图中。当我查看浏览器时,它已成功滚动到视图中 wait = new WebDriverWait(driver, TimeSpan.FromSeconds(WaitTimeout)); var nextPageButton = wait.Until(ExpectedConditions.ElementToBeClickable(NextPageButtonSelector)); ((I

我正在通过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();

另外,javascript元素.click()和Actions类都不起作用。

通过在单击之前添加一个Thread.Sleep调用解决了我自己的问题。我知道这是不推荐的,但实际上这是我能找到的唯一解决方案(


首先向下滚动,然后等待clickable@Infern0我只是尝试了你的想法,但没有成功。在等待调用之前滚动到视图时出现了相同的异常。为什么在e2e测试中操纵css样式?这不是一个好的做法!用户不会这么做-我猜。@nilsK我正在刮取第三方网站的公共代理列表。有时,下一页按钮被其他元素稍微遮住了。(我可以在浏览器中轻松手动单击,但Selenium通过最细微的重叠进行了调整)更改CSS修复了该问题。
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();