Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PYTHON:Selenium循环Selenium.common.exceptions.TimeoutException_Python_Selenium_Selenium Webdriver - Fatal编程技术网

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

PYTHON:Selenium循环Selenium.common.exceptions.TimeoutException,python,selenium,selenium-webdriver,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 WebDrive

我试图从每个单独的产品页面中获取产品详细信息。我尝试在“元素”的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)
回溯(最近一次呼叫最后一次):
文件“/Users/reezalaq/PycharmProjects/legasimall/skinny/getproducts.py”,第70行,在
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--load>div:n子级('+j+)>a'))
文件“/Users/reezalaq/PycharmProjects/legasimall/venv/lib/python3.7/site packages/selenium/webdriver/support/wait.py”,第80行,直到
引发TimeoutException(消息、屏幕、堆栈跟踪)
selenium.common.Exception.TimeoutException:消息:

您的代码的问题是

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

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

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

或者

driver.switch_to.default_content()
或者,在切换到新车窗之前保存原始车窗把手,并使用该把手将驾驶员切换回新车窗

driver.switch_to.default_content()