简体   繁体   English

在 selenium 中替代 time.sleep() 使用 python 而 web 抓取?

[英]Alternative to time.sleep() in selenium using python while web scraping?

I need to scrape price of certain listed food items basis different locations in the country.我需要根据该国不同地点的某些列出的食品的价格。 There's an input text box that allows me to enter the name of the city & pressing "Enter" shows me the list of items available in that city.有一个输入文本框,允许我输入城市的名称,然后按“Enter”显示该城市可用的项目列表。

Here's how I am trying to automate this:这是我尝试自动化的方法:

driver.get("https://grofers.com/")
ele = driver.find_element_by_xpath("//input[@data-test-id='area-input-box']")`
ele.send_keys(area)
ele.send_keys(Keys.RETURN)

Here's the HTML I'm working with:这是我正在使用的 HTML:

 <div style="margin-left: 51px; height: 36px;"> <div style="display: flex; height: 100%;"> <button class="btn location-box mask-button">Detect my location</button> <div class="oval-container"> <div class="oval"> <span class="separator-text"> <div class="or">OR</div> </span> </div> </div> <div style="width: 220px;"> <div class="modal-right__input-wrapper"> <div class="display--table full-width"> <div class="display--table-cell full-width"> <div id="map-canvas"></div> <div class="Select location-search-input-v1 is-searchable Select--single"> <div class="Select-control"> <div class="Select-multi-value-wrapper" id="react-select-2--value"> <div class="Select-placeholder">Type your city Society/Colony/Area</div> <div class="Select-input" style="display: inline-block;">**<input data-test-id="area-input-box" aria-activedescendant="react-select-2--value" aria-expanded="false" aria-haspopup="false" aria-owns="" role="combobox" value="">**</div> </div> <span class="Select-arrow-zone"><span class="Select-arrow"></span></span> </div> </div> </div> </div> </div> </div> </div>

The problem is - after send_keys, the website takes time to autofill the input box AFTER WHICH I need to press enter.问题是 - 在 send_keys 之后,网站需要时间来自动填充输入框,然后我需要按回车键。

I tried using time.sleep(2) after send_keys but this leads to pop-up disappearing & a StaleElementException when I do Keys.RETURN.我尝试在 send_keys 之后使用 time.sleep(2) 但这会导致弹出窗口消失并且在我执行 Keys.RETURN 时出现 StaleElementException。

Have been stuck on this for quite some time now.现在已经坚持了很长一段时间。 Any help/pointers would be appreciated.任何帮助/指点将不胜感激。

I need to scrape price of certain listed food items basis different locations in the country.我需要根据该国不同的地点来计算某些列出的食品的价格。 There's an input text box that allows me to enter the name of the city & pressing "Enter" shows me the list of items available in that city.有一个输入文本框允许我输入城市的名称并按“Enter”显示该城市可用的项目列表。

Here's how I am trying to automate this:这是我试图自动化的方法:

driver.get("https://grofers.com/")
ele = driver.find_element_by_xpath("//input[@data-test-id='area-input-box']")`
ele.send_keys(area)
ele.send_keys(Keys.RETURN)

Here's the HTML I'm working with:这是我正在使用的 HTML:

 <div style="margin-left: 51px; height: 36px;"> <div style="display: flex; height: 100%;"> <button class="btn location-box mask-button">Detect my location</button> <div class="oval-container"> <div class="oval"> <span class="separator-text"> <div class="or">OR</div> </span> </div> </div> <div style="width: 220px;"> <div class="modal-right__input-wrapper"> <div class="display--table full-width"> <div class="display--table-cell full-width"> <div id="map-canvas"></div> <div class="Select location-search-input-v1 is-searchable Select--single"> <div class="Select-control"> <div class="Select-multi-value-wrapper" id="react-select-2--value"> <div class="Select-placeholder">Type your city Society/Colony/Area</div> <div class="Select-input" style="display: inline-block;">**<input data-test-id="area-input-box" aria-activedescendant="react-select-2--value" aria-expanded="false" aria-haspopup="false" aria-owns="" role="combobox" value="">**</div> </div> <span class="Select-arrow-zone"><span class="Select-arrow"></span></span> </div> </div> </div> </div> </div> </div> </div>

The problem is - after send_keys, the website takes time to autofill the input box AFTER WHICH I need to press enter.问题是 - 在 send_keys 之后,网站需要时间来自动填充输入框,然后我需要按 enter。

I tried using time.sleep(2) after send_keys but this leads to pop-up disappearing & a StaleElementException when I do Keys.RETURN.我尝试在 send_keys 之后使用 time.sleep(2) 但这会导致弹出窗口消失并在我执行 Keys.RETURN 时出现 StaleElementException。

Have been stuck on this for quite some time now.已经坚持了很长一段时间了。 Any help/pointers would be appreciated.任何帮助/指针将不胜感激。

Selenium actually has an article on this with Explicit and Implicit waits, I think this is the one you're looking for: Selenium 实际上有一篇关于显式和隐式等待的文章,我想这就是您要找的:

# Wait until an element with id='myNewInput' has class 'myCSSClass'
wait = WebDriverWait(driver, 10)
element = wait.until(element_has_css_class((By.ID, 'myNewInput'), "myCSSClass"))

https://selenium-python.readthedocs.io/waits.html That's the article https://selenium-python.readthedocs.io/waits.html就是这篇文章

You can also create custom wait conditions when none of the previous convenience methods fit your requirements.当以前的便捷方法都不符合您的要求时,您还可以创建自定义等待条件。 A custom wait condition can be created using a class with call method which returns False when the condition doesn't match.可以使用带有调用方法的 class 创建自定义等待条件,该方法在条件不匹配时返回 False。

 class element_has_css_class(object): """An expectation for checking that an element has a particular css class. locator - used to find the element returns the WebElement once it has the particular css class """ def __init__(self, locator, css_class): self.locator = locator self.css_class = css_class def __call__(self, driver): element = driver.find_element(*self.locator) # Finding the referenced element if self.css_class in element.get_attribute("class"): return element else: return False # Wait until an element with id='myNewInput' has class 'myCSSClass' wait = WebDriverWait(driver, 10) element = wait.until(element_has_css_class((By.ID, 'myNewInput'), "myCSSClass"))

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

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