【发布时间】:2020-07-20 17:40:32
【问题描述】:
我需要问题帮助。 我的“for”循环没有继续。
我错过了这个循环还是我应该做些什么来继续这个循环? 该代码用于从网站中提取联系人。访问 url 后,代码将打开第二个窗口。我第一次设法从第二个窗口中提取代码,但“for”循环没有继续。我应该怎么做才能让这条领带继续下去?下面是代码:
class Extrator_olx:
def __init__(self):
chrome_options = Options()
chrome_options.add_argument('--lang=pt-BR')
chrome_options.add_argument('--disable-notifications')
self.driver = webdriver.Chrome(executable_path=r'C:\Users\Daniel pc\Desktop\Tutorial chronDriver\chromedriver.exe', options=chrome_options)
def Iniciar(self):
self.driver.get('https://mg.olx.com.br/regiao-de-juiz-de-fora/regiao-de-juiz-de-fora/autos-e-pecas/carros-vans-e-utilitarios')
window_before = self.driver.window_handles[0]
time.sleep(30)
listado_pagina = self.driver.find_elements_by_xpath('//h2[@class="fnmrjs-10 deEIZJ"]')
for lista_do_for in listado_pagina:
self.driver.execute_script("arguments[0].click()", lista_do_for)
self.driver.implicitly_wait(40)
window_after = self.driver.window_handles[1]
self.driver.switch_to_window(window_after)
self.extrair_d_pg()
def extrair_d_pg(self):
descricao_carro = self.driver.find_element_by_xpath('//h1[@class="sc-ifAKCX sc-1q2spfr-0 fxvTMe"]')
preco_carro = self.driver.find_element_by_xpath('//h2[@class="sc-ifAKCX sc-1leoitd-0 buyYie"]')
vendedor_olx= self.driver.find_element_by_xpath('//span[@class="sc-ifAKCX sc-jhAzac sc-dYzWWc gcvYTZ sc-epGmkI hvWJHG"]')
preco_carro_tx = preco_carro.text
vendedor_olx_tx= vendedor_olx.text
descricao_carro_tx = descricao_carro.text
print(f'{descricao_carro_tx} | {preco_carro_tx} | {vendedor_olx_tx}')
time.sleep(10)
self.driver.close()
#print(preco_carro.text)
#print(vendedor_olx.text)
curso =Extrator_olx()
curso.Iniciar()
【问题讨论】:
-
您是否验证过
listado_pagina中有任何内容?
标签: python selenium loops for-loop window