Get a page with Selenium but wait for unknown element value to not be empty

故事扮演 提交于 2020-12-15 05:27:19

问题


Context

This is a repost of Get a page with Selenium but wait for element value to not be empty, which was Closed without any validity so far as I can tell.

The linked answers in the closure reasoning both rely on knowing what the expected text value will be. In each answer, it explicitly shows the expected text hardcoded into the WebDriverWait call. Furthermore, neither of the linked answers even remotely touch upon the final part of my question:

[whether the expected conditions] come before or after the page Get

"Duplicate" Questions

  • How to extract data from the following html?
  • Assert if text within an element contains specific partial text

Original Question

I'm grabbing a web page using Selenium, but I need to wait for a certain value to load. I don't know what the value will be, only what element it will be present in.

It seems that using the expected condition text_to_be_present_in_element_value or text_to_be_present_in_element is the most likely way forward, but I'm having difficulty finding any actual documentation on how to use these and I don't know if they come before or after the page Get:

webdriver.get(url)

Rephrase

How do I get a page using Selenium but wait for an unknown text value to populate an element's text or value before continuing?


回答1:


I'm sure that my answer is not the best one but, here is a part of my own code, which helped me with similar to your question.

In my case I had trouble with loading time of the DOM. Sometimes it took 5 sec sometimes 1 sec and so on.

url = 'www.somesite.com'
browser.get(url)

Because in my case browser.implicitly_wait(7) was not enought. I made a simple for loop to check if the content is loaded.

some code...

 for try_html in range(7):
        """ Make 7 tries to check if the element is loaded """
        browser.implicitly_wait(7)
        html = browser.page_source
        soup = BeautifulSoup(html, 'lxml')
        raw_data = soup.find_all('script', type='application/ld+json')
        

       """if SKU in not found in the html page we skip 
         for another loop, else we break the 
          tryes and scrape the page"""

        if 'sku' not in html:
            continue
        else:
            scrape(raw_data)
            break

It's not perfect, but you can try it.



来源:https://stackoverflow.com/questions/64976243/get-a-page-with-selenium-but-wait-for-unknown-element-value-to-not-be-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!