【问题标题】:How to use Wait for loading g-loading-icon of Google People Also Ask in Selenium with python?如何使用等待加载 Google People Also Ask in Selenium with python的g-loading-icon?
【发布时间】:2021-11-21 11:43:19
【问题描述】:

首先。我非常感谢@cruisepandey 在这个话题上帮助了我: How to crawl question and answer of Google People Also Ask with Selenium and Python?

所以我是这样使用他的代码的:

    driver = webdriver.Chrome(driver_path)
    driver.maximize_window()
    driver.implicitly_wait(30)
    wait = WebDriverWait(driver, 30)
    
    driver.get("https://www.google.com/search?q=How%20to%20make%20bakery%3F&source=hp&ei=j0aZYYjRAvja2roPrcWcyAU&iflsig=ALs-wAMAAAAAYZlUn4NMUPjfIpQmrXSmjIDnaWjJXWIJ&ved=0ahUKEwjI1JDn0Kf0AhV4rVYBHa0iB1kQ4dUDCAc&uact=5&oq=How%20to%20make%20bakery%3F&gs_lcp=Cgdnd3Mtd2l6EAMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBNQAFgAYJMDaABwAHgAgAF-iAF-kgEDMC4xmAEAoAECoAEB&sclient=gws-wiz")
    
    all_questions = driver.find_elements(By.XPATH, "//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved]")
    print(len(all_questions))
    
    j = 1
    for question in all_questions:
        time.sleep(1)
        ele = driver.find_element(By.XPATH, f"(//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved])[{j}]")
        j = j + 2
        ele.click()
        time.sleep(1)
        answer = ele.find_element(By.XPATH, ".//../following-sibling::div").get_attribute('innerText')
        print(answer)
        print('--------------')

这段代码很有帮助。但我想问两个问题。

  1. 点击显示答案时。如果不使用 time.sleep(1),我会使用 wait until answer show,如何准确获取 class 和 code to wait answer show?
  2. 如果网速慢,代码会出现问题。单击更多结果时不会显示。我尝试使用等到 invisibility_of_element_located 加载图标。但我发现 Xpath 不对。有什么办法吗? 这是我对@cruisepandey 代码的代码更新:
    driver = webdriver.Chrome(driver_path)
    driver.maximize_window()
    driver.implicitly_wait(30)
    wait = WebDriverWait(driver, 30)
    
    driver.get("https://www.google.com/search?q=How%20to%20make%20bakery%3F&source=hp&ei=j0aZYYjRAvja2roPrcWcyAU&iflsig=ALs-wAMAAAAAYZlUn4NMUPjfIpQmrXSmjIDnaWjJXWIJ&ved=0ahUKEwjI1JDn0Kf0AhV4rVYBHa0iB1kQ4dUDCAc&uact=5&oq=How%20to%20make%20bakery%3F&gs_lcp=Cgdnd3Mtd2l6EAMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBNQAFgAYJMDaABwAHgAgAF-iAF-kgEDMC4xmAEAoAECoAEB&sclient=gws-wiz")
    
    all_questions = driver.find_elements(By.XPATH, "//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved]")
    print(len(all_questions))
    
    j = 1
    for question in all_questions:
        time.sleep(1)
        ele = driver.find_element(By.XPATH, f"(//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved])[{j}]")
        j = j + 2
        ele.click()
    
        # Question 1: To waiting answer show.
        timeout  = 30
        answer_css_class = 'wWOJcd'
        try:
    
            is_question_show = WebDriverWait(driver, timeout).until(
                EC.presence_of_element_located((By.CLASS_NAME, answer_css_class))
            )
        except TimeoutException:
            pass
    
        time.sleep(1)
        answer = ele.find_element(By.XPATH, ".//../following-sibling::div").get_attribute('innerText')
    
        # Question 2: To waiting G loading icon when click more answer with slow internet.
        loading_element_xpath = '/html/body/div[7]/div/div[9]/div[1]/div/div[2]/div[2]/div/div/div[2]/div/div/div[1]/g-loading-icon'
        try:
    
            is_question_show = WebDriverWait(driver, timeout).until(
                EC.presence_of_element_located((By.XPATH, loading_element_xpath))
            )
        except TimeoutException:
            pass
    
        print(answer)
        print('--------------')

那么有什么办法不使用 time.sleep 并使用 wait until in selenium?

【问题讨论】:

    标签: python selenium selenium-webdriver xpath wait


    【解决方案1】:

    我认为您不需要invisibility_of_element_located 来加载图标。

    你可以有一个单独的尝试——除了问题和答案。

    代码:

    driver = webdriver.Chrome(driver_path)
    driver.maximize_window()
    wait = WebDriverWait(driver, 30)
    
    
    driver.get("https://www.google.com/search?q=How%20to%20make%20bakery%3F&source=hp&ei=j0aZYYjRAvja2roPrcWcyAU&iflsig=ALs-wAMAAAAAYZlUn4NMUPjfIpQmrXSmjIDnaWjJXWIJ&ved=0ahUKEwjI1JDn0Kf0AhV4rVYBHa0iB1kQ4dUDCAc&uact=5&oq=How%20to%20make%20bakery%3F&gs_lcp=Cgdnd3Mtd2l6EAMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBMyBAgAEBNQAFgAYJMDaABwAHgAgAF-iAF-kgEDMC4xmAEAoAECoAEB&sclient=gws-wiz")
    
    all_questions = driver.find_elements(By.XPATH, "//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved]")
    print(len(all_questions))
    
    j = 1
    for question in all_questions:
        try:
            ele = wait.until(EC.visibility_of_element_located((By.XPATH, f"(//span[text()='People also ask']/../following-sibling::div/descendant::div[@data-hveid and @class and @jsname and @data-ved])[{j}]")))
            ele.click()
            j = j + 2
        except:
            print("Could not click on Question link")
        try:
            answer = ele.find_element(By.XPATH, ".//../following-sibling::div").get_attribute('innerText')
            print(answer)
        except:
            print("Could not read answer.")
    

    进口:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

    输出:

    6
    Follow the below-mentioned steps to open a successful bakery business in India in 2021:
    Create A Bakery Business Plan. ...
    Choose A Location For Your Bakery Business. ...
    Get All Licenses Required To Open A Bakery Business In India. ...
    Get Manpower Required To Open A Bakery. ...
    Buy Equipment Needed To Start A Bakery Business.
    More items...
    
    A Detailed Guide On How To Start A Bakery Business In India
    https://www.posist.com › Home › Resources
    Search for: How do I start my own bakery?
    The most profitable bakeries have a gross profit margin of 9%, while the average is much lower at 4%. The growth of profitable bakeries can be as high as 20% year over year. While a large number of bakeries never reach the break-even, a handful of them can even have a net profit margin as high as 12%.06-Jul-2020
    
    How Much Do Bakery Owners Make? | Restaurant Accounting
    https://restaurantaccounting.net › how-much-do-bakery-o...
    Search for: Do bakeries make money?
    Home Bakery Business - How to Start
    Decide on the goods to bake. ...
    Plan your kitchen space. ...
    Get a permit. ...
    Talk to a tax agent. ...
    Set appropriate prices. ...
    Start baking and selling.
    16-Feb-2015
    
    Home Bakery Business - How to Start | BakeCalc
    http://www.bakecalc.com › blog › how-to-start-a-home-b...
    Search for: How do I start a small baking business from home?
    
    
    The success of any bakery, whether a home-based or commercial operation, hinges largely on the quality of the products. ... Creating a niche for your bakery, such as stunning cakes or unusual pastries, can help set you apart and build a loyal customer base.
    
    What Makes a Home Bakery Business Successful?
    https://smallbusiness.chron.com › home-bakery-business-s...
    Search for: What makes a bakery successful?
    
    Process finished with exit code 0
    

    【讨论】:

    • 是的!谢谢@cruisepandey。我会尝试并研究更多关于visibility_of_element_located的信息。非常感谢。有任何关于 catch Xpath 的帖子或文档可以阅读吗?我看到你对 Xpath 的理解太专业了
    • 请使用此链接了解有关 XPath 的更多信息w3schools.com/xml/xpath_axes.asp
    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 2017-07-05
    • 2017-03-27
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    相关资源
    最近更新 更多