PYTHON:Selenium循环selenium.common.exceptions.TimeoutException

时间:2019-11-03 06:41:36

标签: python selenium selenium-webdriver

我正在尝试从每个产品页面中抓取产品详细信息。我尝试在xpath之间为“元素”追加一个整数。对于范围 j = 1 的for i in range循环运行没有问题,显示 getproductname ,但对于 j = 2 则抛出错误。

如何修复循环?我不知道这里似乎是什么问题:

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

CHROMEDRIVER_PATH = '/Users/reezalaq/PycharmProjects/wholesale/driver/chromedriver'
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(CHROMEDRIVER_PATH, options=chrome_options)
chrome_options.accept_untrusted_certs = True
chrome_options.assume_untrusted_cert_issuer = True
chrome_options.headless = False

driver.get('https://www.skinnymixes.com/collections/skinny-syrups')
for i in range(1,20):
    j = str(i)
    print(j)
    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, '#collection-content > div.collection-listing > div > div.row.product-list.text-center.mb-3.in-view.in-view--active.in-view--loaded > div:nth-child('+ j +') > a')))
    print(elements)
    for element in elements:
        url = element.get_attribute('href')
        print(url)
        #open new tab with specific url
        driver.execute_script("window.open('" +url +"');")
        #switch to new tab
        driver.switch_to.window(driver.window_handles[1])
        getproductname = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="shopify-section-product"]/div/div[1]/div/div[2]/h1')))
        print(getproductname.text)
Traceback (most recent call last):
  File "/Users/reezalaq/PycharmProjects/legasimall/skinny/getproducts.py", line 70, in <module>
    elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, '#collection-content > div.collection-listing > div > div.row.product-list.text-center.mb-3.in-view.in-view--active.in-view--loaded > div:nth-child('+ j +') > a')))
  File "/Users/reezalaq/PycharmProjects/legasimall/venv/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

1 个答案:

答案 0 :(得分:0)

您的代码存在的问题是

  • 它将在新窗口中打开产品,将驱动程序切换至该产品,然后抓取信息

  • ,但此后不会将驱动程序切换回原始窗口。

因此,基本上,在循环结束时,您需要将其切换回原始窗口,即

要么

driver.switch_to.default_content()

或者,在切换到新窗口之前保存原始窗口句柄,然后使用该句柄将驱动程序切换回它。