简体   繁体   English

等到页面刷新

[英]wait until page refreshed

Here is a description of what I am trying to do using Selenium Webdriver in Python: I have my website which takes as input various parameters for a specific product and outputs a price for the product with those parameters. 这是我要在Python中使用Selenium Webdriver尝试做的事情的描述:我有一个网站,该网站将特定产品的各种参数作为输入,并使用这些参数输出该产品的价格。 I am keeping all but one parameters constant and varying one specific parameter in a for loop to see how the price varies according to that one specific parameter. 我将除一个参数外的所有参数保持不变,并在for循环中更改一个特定参数,以查看价格如何根据该特定参数变化。 Once i change the parameter i submit the form and then i use implicitly wait as follows: 一旦更改参数,我将提交表单,然后隐式使用,如下所示:

submit_btn.click()
driver.implicitly_wait(10)  
driver.find_element_by_name("Buy_Product")
soup=BeautifulSoup(driver.page_source)

When entering the first set of parameters the page is clear and it does not contain the buttton with name "Buy_Product" so i am using the line 输入第一组参数时,页面将很清晰,并且不包含名称为“ Buy_Product”的按钮,因此我正在使用该行

driver.find_element_by_name("Buy_Product")

to make sure the code waits for that button to appear which will mean the page now contains the price i want to extract. 确保代码等待该按钮出现,这意味着该页面现在包含我要提取的价格。 The problem is that the second time through the loop when i vary the parameter and try to get the new price the button "Buy_Product" is already there so implicitly wait does not work anymore and sometimes it will take the previous page_source before the price has time to be updated. 问题是,当我更改参数并尝试获取新价格时,第二次通过循环,按钮“ Buy_Product”已经存在,因此隐式等待不再起作用,有时它将在价格有时间之前占用先前的page_source要被更新。 The tricky part is that sometimes even for different parameters the price is the same so i can not just check whether the visible text of the price has changed. 棘手的是,有时即使对于不同的参数,价格都是相同的,所以我不能仅检查价格的可见文本是否已更改。 Any ideas how this can be solved without using time.sleep? 有什么想法可以在不使用time.sleep的情况下解决该问题吗?

The situation is something tricky here...i'm not sure...if this helps...but give a try... 这里的情况有些棘手...我不确定...是否有帮助...但请尝试一下...

My suggestion is on JavaScript injection, where i check if the Page is loaded completely and it waits till then. 我的建议是关于JavaScript注入,在这里我检查Page是否已完全加载,并一直等到那时。 (c# code snippet) (C#代码段)

IWait<IWebDriver> Driver_Wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
Driver_Wait.Until(Driver => ((IJavaScriptExecutor)JS_Driver).ExecuteScript("return document.readyState").Equals("complete"));

I hope this helps....All the Best :-) 希望对您有帮助。。。

If Anuragh27crony's solution doesn't work for you, you may also want to think about waiting for jquery to be inactive. 如果Anuragh27crony的解决方案对您不起作用,您可能还需要考虑等待jquery处于非活动状态。 The code will look mostly the same, but pass in this javascript call instead: 该代码看起来大致相同,但是传递了以下javascript调用:

ExecuteScript("return jQuery.active == 0")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM