在页面完全加载之前,如何在元素上点击selenium并抓取数据?我的互联网连接非常糟糕,因此有时需要永久地加载页面,无论如何都在这周围?
python selenium webdriver selenium-webdriver pageloadstrategy
我正在使用Chrome浏览器测试WebApp.
有时页面会在很长时间后加载.我需要停止下载或限制下载时间.
在FireFox我知道PAGE_LOAD_STRATEGY = "eager".
铬有类似之处吗?
PS:driver.manage().timeouts().pageLoadTimeout()有效,但之后对Webdriver的任何处理都会引发TimeOutException.我需要在停止启动后获取页面的当前URL.
selenium webdriver selenium-chromedriver selenium-webdriver pageloadstrategy
这是我第一次在 stackoverflow 上发帖,我对 Selenium 和 Python 还有些陌生。
当 URL 等于 fx: https://www.example.com时,我不希望运行函数。
我在另一个讨论中读过 这个答案,但我不太明白发生了什么。
我希望您能抽出时间回答我的问题。
好的,所以我刚刚尝试过这个:
driver.get('https://www.google.com')
time.sleep(4)
driver.get('https://www.stackoverflow.com')
if WebDriverWait(driver, 10).until(EC.url_to_be('https://stackoverflow.com')):
print('Desired url was rendered within allocated time')
else:
print('Desired url was not rendered within allocated time')
Run Code Online (Sandbox Code Playgroud)
但这没有用。有任何想法吗?
控制台说
Traceback (most recent call last):
File "/Users/holger/PycharmProjects/waitTest/wait.py", line 15, in <module>
if WebDriverWait(browser, 10).until(EC.url_to_be('https://www.stackoverflow.com')):
File "/Users/holger/PycharmProjects/waitTest/venv/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Run Code Online (Sandbox Code Playgroud) 我在 Java 中使用 selenium 使用以下代码。我添加了 4 秒的 pageLoadTimeout,但是,驱动程序继续等待,直到加载完整页面。有什么帮助吗?
System.setProperty("webdriver.gecko.driver", System.getProperty("user.home") + "\\Desktop\\geckodriver.exe");
FirefoxBinary b = new FirefoxBinary(new File(System.getProperty("user.home") + "\\desktop\\Mozilla Firefox\\firefox.exe"));
FirefoxOptions options = new FirefoxOptions().setBinary(b);
driver = new FirefoxDriver(options);
//driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(4, TimeUnit.SECONDS);
driver.get("https://www.booking.com/hotel/in/the-taj-mahal-palace-tower.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYATG4AQbIAQzYAQHoAQH4AQKSAgF5qAID;sid=338ad58d8e83c71e6aa78c67a2996616;dest_id=-2092174;dest_type=city;dist=0;group_adults=2;hip_dst=1;hpos=1;room1=A%2CA;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl");
List<WebElement> facilitySectionList = driver.findElements(By.className("facilitiesChecklistSection"));
Run Code Online (Sandbox Code Playgroud) 单击按钮后未找到链接或任何验证消息,测试用例就会失败。
我使用显式等待页面加载:
var waitForDocumentReady = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(10));
waitForDocumentReady.Until((wdriver) => (WebDriver as IJavaScriptExecutor).ExecuteScript("return document.readyState").Equals("complete"));
Run Code Online (Sandbox Code Playgroud)
对于等待验证消息的特定 div:
WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(10));
wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.ClassName("validationErrors")));
Run Code Online (Sandbox Code Playgroud)
但测试用例有时会通过管道,有时会失败。
selenium timeoutexception selenium-webdriver azure-devops azure-pipelines
什么是硒?
当您打开Selenium的官方页面时,您首先读到的是“什么是Selenium?”中的“ Selenium automates browser”。部分。“硒的哪个部分适合我?”部分 下面提供了Selenium WebDriver和Selenium IDE之间的选择。据此,我推断出Selenium是工具的集合,该集合包括IDE,WebDriver API(语言绑定),Grid,Selenium Standalone Server,浏览器驱动程序。必须下载适当的文件才能构建项目。
什么是WebDriver?
WebDriver是一个API。它用多种语言编写,这些语言称为语言绑定。API具有控制浏览器的功能。您可以使用这些功能编写脚本来以所需的方式(测试用例)控制浏览器。
这就是我所知道的。如果我错了,请纠正我。我想从面试的角度知道这两个问题的答案。
我正在尝试让 selenium 使用 .readyState 等待页面完全加载,但在继续之前我找不到让 selenium 测试网页的 .readyState 的正确方法。
两条散列出来的行是我使其工作的最佳尝试,包括下面链接中的示例;其中“.equals”似乎没有任何效果。
python-selenium 有没有办法等到页面的所有元素都加载完毕?
当运行带有散列输出行的代码时,输出将打印“正在加载”并引发错误,因为打印函数之后的元素尚未加载。
注意:使用等待和预期条件可以轻松解决此问题,但问题在于使用 .readyState。
ps:我的理解是,当 pageLoadStrategy 设置为“normal”时,selenium 默认等待 .readyState 为“complete”,但是一旦驱动程序启动,pageLoadStrategy 就无法在“none”和“normal”之间切换。如果有人不知道,那么这可能是一个可能的解决方案。
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
caps = DesiredCapabilities().CHROME.copy()
caps["pageLoadStrategy"] = "none"
driver = webdriver.Chrome(
desired_capabilities=caps,
executable_path=r'C:\chromedriver_win32\chromedriver.exe'
)
driver.get("https://www.google.com/maps")
# driver.execute_script("return document.readyState").equals("complete"))
# driver.execute_script("return document.readyState == 'complete'")
print(driver.execute_script("return document.readyState"))
driver.find_element_by_xpath('//*[@id="searchboxinput"]').send_keys("somewhere")
Run Code Online (Sandbox Code Playgroud) 我被困在我希望我的脚本等待 ajax 请求完成的地方。
我尝试过: 1. 在慢速网络模式下运行应用程序,单击创建按钮并检查 JQuery.active 返回 1. 仅发生 ajax 调用。
问题是整个页面没有刷新。它只是页面上的一些数据每次都会更新。
我使用显式等待来等待加载程序/进度条消失,但我被要求专门等待 ajax 完成,因为 UI 可能很快就会改变。
我尝试实施:
public boolean waitForJSandJQueryToLoad() {
WebDriverWait wait = new WebDriverWait(driver, 30);
// wait for jQuery to load
ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
try {
return ((Long)((JavascriptExecutor)getDriver()).executeScript("return jQuery.active") == 0);
}
catch (Exception e) {
// no jQuery present
return true;
}
}
};
// wait for Javascript to load
ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() { …Run Code Online (Sandbox Code Playgroud) 我尝试使用 selenium 和 python3.x 在 UI 上编写流程。在某些情况下,我想等待特定查询完成后再进行下一步。因为在查询完成期间,没有我可以选择使用的元素,并且return document.readyStateascomplete对我不起作用。是否可以使用 selenium wait.until 来做到这一点?喜欢
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
WebDriverWait(driver, 120).until(lambda x: "query return 0")
WebDriverWait(driver, 120).until(EC."something to wait for")
Run Code Online (Sandbox Code Playgroud) selenium ×8
webdriver ×5
python ×3
ajax ×1
azure-devops ×1
java ×1
python-3.x ×1
readystate ×1
url ×1