Python 3.x 如何点击";“下一步”;按钮,直到它不再存在-Python、Selenium、请求
我正在从已分页的网页中抓取数据,一旦我完成抓取一页,我需要单击“下一步”按钮并继续抓取下一页。然后,我需要停止一旦我刮掉了所有的页面和一个下一步按钮不再存在。下面包含我需要单击的“下一步”按钮周围的htmlPython 3.x 如何点击";“下一步”;按钮,直到它不再存在-Python、Selenium、请求,python-3.x,selenium,beautifulsoup,python-requests,Python 3.x,Selenium,Beautifulsoup,Python Requests,我正在从已分页的网页中抓取数据,一旦我完成抓取一页,我需要单击“下一步”按钮并继续抓取下一页。然后,我需要停止一旦我刮掉了所有的页面和一个下一步按钮不再存在。下面包含我需要单击的“下一步”按钮周围的html <tr align="center"> <td colspan="8" bgcolor="#FFFFFF"> <br> <span class="paging"> <b> -- Page
<tr align="center">
<td colspan="8" bgcolor="#FFFFFF">
<br>
<span class="paging">
<b> -- Page 1 of 3 -- </b>
</span>
<p>
<span class="paging">
<a href="page=100155&by=state&state=AL&pagenum=2"> .
<b>Next -></b>
</a>
</span>
<span class="paging">
<a href=" page=100155&by=state&state=AL&pagenum=3">Last ->></a>
</span>
</p>
</td>
</tr>
我在网上找到的所有解决方案都不起作用,主要以以下错误结束:
ElementClickInterceptedException: Message: element click
intercepted: Element <a href="?
page=100155&by=state&state=AL&pagenum=2">...</a> is not
clickable at point (119, 840). Other element would receive the
click: <body class="custom-background hfeed" style="position:
relative; min-height: 100%; top: 0px;">...</body>
(Session info: chrome=76.0.3809.132)
element单击拦截异常:消息:element单击
截获:元素未被截获
可点击点(119840)。其他元素将收到
点击:。。。
(会话信息:chrome=76.0.3809.132)
如果错误代码的剩余部分有助于审查,请让我知道,我将用此错误更新帖子
我查看了以下资源,但均无效:
有谁能给我一些建议,告诉我如何选择“下一步”按钮(如果存在)并使用这组HTML进入下一页?如果您需要进一步澄清,请告诉我。听起来您在问两个不同的问题:
public static void ExecuteJavaScriptClickButton(this IWebDriver driver, IWebElement element)
{
((IJavaScriptExecutor) driver).ExecuteScript("arguments[0].click();", element);
}
在上面的代码中,您必须将WebDriver
实例转换为IJavascriptExecutor
,这允许您通过Selenium运行JS代码。参数element
是您希望单击的元素——在本例中是Next按钮
根据您的代码示例,您的Javascript单击可能如下所示:
var nextButton = driver.findElement(By.LINK_TEXT, "Next ->"));
driver.ExecuteJavascriptClickButton(nextButton);
现在,转到另一个问题——单击按钮直到按钮不再可见。我将在while
循环中实现这一点,该循环在下一个按钮不再存在时中断。我还建议实现一个功能,可以检查下一步按钮是否存在,并在按钮不存在的情况下忽略ElementNotFound
或nosucheelement
异常,以避免中断测试。下面是一个包含元素exists
实现的示例:
public bool ElementExists(this IWebDriver driver, By by)
{
// attempt to find the element -- return true if we find it
try
{
return driver.findElements(by).Count > 0;
}
// catch exception where we did not find the element -- return false
catch (Exception e)
{
return false;
}
}
public void ClickNextUntilInvisible()
{
while (driver.ElementExists(By.LINK_TEXT, "Next ->"))
{
// find next button inside while loop so it does not go stale
var nextButton = driver.findElement(By.LINK_TEXT, "Next ->"));
// click next button using javascript
driver.ExecuteJavascriptClickButton(nextButton);
}
}
此while
循环检查每次迭代中是否存在下一个按钮。如果按钮不存在,循环将中断。在循环内部,我们通过每次连续单击调用driver.findElement
,这样就不会得到StaleElementReferenceException
希望这有帮助。看起来是另一个元素收到了单击,而不是下一个元素。我建议您尝试向下滚动到该元素,然后尝试单击它。因为它位于body标记上,所以这可能是一个弹出窗口,用于拦截单击。首先尝试单击标记。(它们通常设置一个标志,允许第二次单击…有时通过更改当前窗口/选项卡的位置并在第二个窗口中打开当前页面来创建“popunder”。)发布标记和/或任何客户端脚本。尝试使用javascript单击它。那通常会有用的。谢谢大家的建议。您是否碰巧有一个可能的代码解决方案可以帮助解决这种情况?我明白你们的意思,但我对selenium比较陌生,不完全确定实现你们建议的最佳方式。
public bool ElementExists(this IWebDriver driver, By by)
{
// attempt to find the element -- return true if we find it
try
{
return driver.findElements(by).Count > 0;
}
// catch exception where we did not find the element -- return false
catch (Exception e)
{
return false;
}
}
public void ClickNextUntilInvisible()
{
while (driver.ElementExists(By.LINK_TEXT, "Next ->"))
{
// find next button inside while loop so it does not go stale
var nextButton = driver.findElement(By.LINK_TEXT, "Next ->"));
// click next button using javascript
driver.ExecuteJavascriptClickButton(nextButton);
}
}