【问题标题】:How to Close a new tab that opened from using the click option?如何关闭使用单击选项打开的新选项卡?
【发布时间】:2020-09-26 08:34:01
【问题描述】:

我正在尝试使用 Selenium 抓取电子商务网站 www.flipkart.com 以下是我正在尝试的步骤:

1. Open Flipkart
2. Close the pop-up login prompt
3. Go to the Search Menu
4. Search for a keyword "Laptop"8/*/-
5. Click on the Search button
6. Open every record as a new tab and scrape information from it.
7. After scraping close the newly opened tab and go to the parent tab.
8. Repeat Steps for all the records on the page.
9. After getting all the records from one page go to the next page and repeat steps.

我的代码存在的问题是我无法从第 6 步关闭新打开的选项卡。

driver=webdriver.Chrome()
driver.get("http://www.flipkart.com")

#Close pop us
elem=driver.find_element_by_xpath("/html/body/div[2]/div/div/button")
elem.click()

#click on search field
#1
elem=driver.find_element_by_xpath("xpath_tag_too_long_to_add")
elem.send_keys("Laptop")

#search
#2
elem=driver.find_element_by_xpath("xpath_tag_too_long_to_add")
elem.click()
time.sleep(10)

#get the maximum page number
y=int(driver.find_element_by_css_selector('div._2zg3yZ').text[10:12])

#scrape pages
name=[]
price=[]
color=[]
for i in range(1,y+1):
    driver.get(link+str(i))
    x=driver.find_elements_by_css_selector('div._3wU53n')
    for i in x:
       i.click()
       actions = ActionChains(driver)      
       driver.switch_to.window(driver.window_handles[1])
       time.sleep(2)
       #3
       names=driver.find_element_by_xpath('xpath_tag_too_long_to_add').text
       name.append(name)
       #4
       prices=driver.find_element_by_xpath('xpath_tag_too_long_to_add').text
       price.append(prices)
       #5
       colors=driver.find_element_by_xpath('xpath_tag_too_long_to_add').text
       color.append(colors)
       driver.switch_to.window(driver.window_handles[0])

我尝试过诸如关闭​​选项卡的操作键之类的方法,但没有奏效。我因为没有关闭新打开的选项卡而遇到的问题是,当我使用 switch_to.window 时,它会不断地与父页面来回切换,而第一个打开的页面在所有列中都给了我相同的值。

      #this is what I have tried
      elem=driver.find_element_by_tag_name("body")
      elem.send_keys(Keys.CONTROL+"w")


     Here are the Xpath & Links:
     #1-/html/body/div[1]/div/div[1]/div[1]/div[2]/div[2]/form/div/div/input
     #2-/html/body/div[1]/div/div[1]/div[1]/div[2]/div[2]/form/div/button
     #3-/html/body/div[1]/div/div[3]/div[2]/div[1]/div[2]/div[2]/div/div[1]/h1/span
     #4-/html/body/div[1]/div/div[3]/div[2]/div[1]/div[2]/div[2]/div/div[3]/div[1]/div/div[1]
     #5-/html/body/div[1]/div/div[3]/div[2]/div[1]/div[2]/div[7]/div[3]
        /div/div[2]/div[1]/div[1]/table/tbody/tr[5]/td[2]/ul/li

链接-https://www.flipkart.com/search?q=laptop&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off&page=

网站链接: https://www.flipkart.com/

感谢阅读。

【问题讨论】:

标签: python selenium selenium-webdriver tabs window-handles


【解决方案1】:

使用get() 打开Flipkart 后,存储父窗口句柄,即current_window_handle

driver.get("https://www.flipkart.com")
parent_window  = driver.current_window_handle

添加颜色后,for() 循环中的下一步,close() 相邻的新选项卡,然后切换到父窗口,如下所示:

for i in x:
    #previous lines of code
    #5
    color.append(colors)
    driver.close()
    driver.switch_to.window(parent_window)

您可以在Selenium Switch Tabs找到相关讨论

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多