Home > Net >  How to make FOR run together with while?
How to make FOR run together with while?

Time:11-25

I have the code below working until the WHILE, I needed to add the WHILE to capture all the information on the page.

However, when the code stops working, I need to know how I can make the code run after the While.

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")
options.add_argument('--disable-notifications')

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 1)

url =     "https://www.google.com.br/maps/search/contabilidade balneario camboriu/@-26.9905418,-48.6289914,1    5z"
driver.get(url)
while True:
    try:
        wait.until(EC.visibility_of_element_located((By.XPATH,     "//span[contains(text(),'reached the end')]")))
        barraRolagem = wait.until(EC.presence_of_element_located((By.XPATH,     "//div[@role='main']//div[@aria-label]")))
        driver.execute_script("arguments[0].scroll(0, arguments[0].scrollHeight);",     barraRolagem)
        break
    except:
        barraRolagem = wait.until(EC.presence_of_element_located((By.XPATH,     "//div[@role='main']//div[@aria-label]")))
        driver.execute_script("arguments[0].scroll(0, arguments[0].scrollHeight);",     barraRolagem)
    time.sleep(0.5)

    classe_empresas = driver.find_elements(By.CLASS_NAME, "hfpxzc")

    for empresa in classe_empresas:
          urls = empresa.get_attribute("href")
          links.append(urls)


  for paginas_individuais in links:
        driver.get(paginas_individuais)  
              
        try:
              print("Telefone")
              tel = driver.find_element(By. XPATH, "//div[contains(text(),'(" ddd ")')]").get_attribute("innerHTML")
              print("Endereco")
              endereco = driver.find_element(By. XPATH, "/html[1]/body[1]/div[3]/div[9]/div[9]/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[7]/div[3]/button[1]/div[1]/div[2]/div[1]").get_attribute("innerHTML")                  
              print("Nome")
              nome = driver.find_element(By. TAG_NAME, "title").get_attribute("innerHTML")

       except:
              print("erro")

        

CodePudding user response:

for paginas_individuais in links: block not worked due to unintended indentation.
This block should be with the same indentation as while True: block to make it performed after the while True: block is completed.

  • Related