首页 > 解决方案 > Python,Selenium 动作链执行额外任务

问题描述

我的程序的目的是使用 selenium 中的操作键单击 parent_window 上的 URL(在 iframe 中),在新选项卡中打开单击的 URL,在抓取数据后关闭选项卡,然后打开下一个 URL被刮掉。问题是,一旦我尝试打开第二个 URL,它就会打开该 URL 以及之前打开的那个 URL,导致打开 2 个选项卡而不是 1 个。我在 Google Chrome 上。

x = 0
parent_window = driver.current_window_handle

while x < len(url_list): #Going through the URLs on the parent_window
    time.sleep(1)

#(ERROR, OPENING UP 2 TABS once x = 1)
    actions.key_down(Keys.CONTROL).click(url_list[x]).perform()

    #Scrape name and consideration amount
    driver.switch_to.window(driver.window_handles[x+1])
    last_name.append(driver.find_element_by_xpath("/html/body/table[3]/tbody/tr/td[2]/table/tbody/tr[1]/td[2]/font"))
    first_name.append(driver.find_element_by_xpath("/html/body/table[3]/tbody/tr/td[2]/table/tbody/tr[1]/td[3]/font"))
    time.sleep(1)
    driver.close()
    driver.switch_to.window(parent_window)
    driver.switch_to.frame("frmeSearchForm")
    
    x = x+1

标签: pythonseleniumgoogle-chromeselenium-webdriver

解决方案


推荐阅读