【问题标题】:Python Selenium - Cannot close tab using google shortcutsPython Selenium - 无法使用谷歌快捷方式关闭选项卡
【发布时间】:2021-01-17 14:44:39
【问题描述】:

我正在做一个小的 selenium 项目,但遇到了一些问题。

所以我需要做的是单击一个链接以在新选项卡中打开它,每当我获取信息时,我需要关闭该选项卡并进入下一个选项卡。 driver.close() 不起作用,因为它给了我错误:Message: no such window: target window already closed。所以我反而尝试了这个(在研究时看到了这个): driver.find_element_by_tag_name('html').send_keys(Keys.CONTROL + 'w'),我也尝试添加Keys.F4,但没有任何效果。

它似乎对其他人有效,那为什么不适合我呢?

代码:

def cpuFunc():
    i = 0
    print("Launching CPU")
    cpu = webdriver.Chrome('chromedriver.exe',options=option)
    cpu.get('https://www.komplett.se/category/11204/datorutrustning/datorkomponenter/processor')
    cpu.find_element_by_xpath('/html/body/div[1]/div[2]/div[1]/div/div/div[2]/form/div/div[1]/button').click()
    #while i < 10:
    #    cpu.find_element_by_tag_name('html').send_keys(Keys.END)
    #    i += 1
    #    time.sleep(0.5)
    #print("At bottom: CPU")
    cpu.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)
    time.sleep(0.5)
    link = cpu.find_element_by_xpath(f'/html/body/main/div/div[2]/div[5]/div[2]/form/div[1]/a')
    ActionChains(cpu).key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()
    time.sleep(1)
    window = cpu.window_handles[-1]
    cpu.switch_to.window(window)
    title = cpu.find_element_by_xpath("/html/body/div[2]/main/div[2]/div[2]/div[3]/section/div/section/div[1]/h1/span").text
    price = cpu.find_element_by_xpath("/html/body/div[2]/main/div[2]/div[2]/div[3]/section/div/section/div[3]/div[2]/div[1]/div/div/div[1]/div[1]/div[1]/span").text
    btn = cpu.find_element_by_xpath('/html/body/div[2]/main/div[2]/div[2]/div[3]/div/div[2]/div/section[2]/button')
    time.sleep(0.5)
    cpu.execute_script("arguments[0].click();", btn)
    core = cpu.find_element_by_xpath("/html/body/div[2]/main/div[2]/div[2]/div[3]/div/div[2]/div/section[2]/div/div/div/table[2]/tbody/tr[2]/td").text
    thread = cpu.find_element_by_xpath("/html/body/div[2]/main/div[2]/div[2]/div[3]/div/div[2]/div/section[2]/div/div/div/table[2]/tbody/tr[3]/td").text
    cache = cpu.find_element_by_xpath("/html/body/div[2]/main/div[2]/div[2]/div[3]/div/div[2]/div/section[2]/div/div/div/table[2]/tbody/tr[4]/td").text
    clock = cpu.find_element_by_xpath("/html/body/div[2]/main/div[2]/div[2]/div[3]/div/div[2]/div/section[2]/div/div/div/table[2]/tbody/tr[7]/td").text
    turbo = cpu.find_element_by_xpath("/html/body/div[2]/main/div[2]/div[2]/div[3]/div/div[2]/div/section[2]/div/div/div/table[2]/tbody/tr[8]/td").text
    socket = cpu.find_element_by_xpath("/html/body/div[2]/main/div[2]/div[2]/div[3]/div/div[2]/div/section[2]/div/div/div/table[2]/tbody/tr[9]/td").text
    wattage = cpu.find_element_by_xpath("/html/body/div[2]/main/div[2]/div[2]/div[3]/div/div[2]/div/section[2]/div/div/div/table[2]/tbody/tr[10]/td").text
    cpu.find_element_by_tag_name('html').send_keys(Keys.CONTROL + 'w') # Here it shall close
    time.sleep(60000)
enter code here

【问题讨论】:

标签: python python-3.x selenium selenium-chromedriver


【解决方案1】:

您可以简单地使用 ActionChains & Keys。

from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

如果是 MacO:

step_1 = ActionChains(cpu)
step_1.send_keys(Keys.COMMAND + 'w')

如果是 Windows:

step_1 = ActionChains(cpu)
step_1.send_keys(Keys.CONTROL + 'w')

希望对你有帮助,如有问题请评论。

【讨论】:

  • 对我不起作用,太奇怪了。我会尝试其他一些我找到的方法,并尝试使用最新的 chromedriver(上次尝试使用最新版本时出现错误)。
  • 你用的是什么版本的chrome和chrome驱动?
  • 87.0.4280.88 @jiya
  • 不,尝试使用 chromedriver 88 对我不起作用,step_1.send_keys(Keys.CONTROL + 'w') 没有任何作用:/
  • 刚刚尝试了 Chromedriver 88,但它对我也不起作用...您尝试过 87 吗?对我来说,它在 87 年就像一种魅力
【解决方案2】:

Driver.close() 在修复了许多容易破坏的完整 xpath 并添加 webdriver 等待稳定查找元素之后为我工作。

wait = WebDriverWait(cpu, 10)
cpu.get('https://www.komplett.se/category/11204/datorutrustning/datorkomponenter/processor')
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@class='btn-large primary'][@type='submit']"))).click()
link = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "form > div:nth-child(1) > a")))
ActionChains(cpu).key_down(Keys.CONTROL).click(link).key_up(Keys.CONTROL).perform()
time.sleep(1)
window = cpu.window_handles[-1]
cpu.switch_to.window(window)
title = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.product-main-info__info > h1 > span"))).text
price = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.product-price > span"))).text
print(title,price)
btn = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "section.product-section.technical-details.col-xs-12 > button")))
btn.click()
table2 = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "table:nth-child(2) > tbody")))
core = table2.find_element_by_xpath("./tr[2]/td").text
thread = table2.find_element_by_xpath("./tr[3]/td").text
cache = table2.find_element_by_xpath("./tr[4]/td").text
clock = table2.find_element_by_xpath("./tr[7]/td").text
turbo = table2.find_element_by_xpath("./tr[8]/td").text
socket = table2.find_element_by_xpath("./tr[9]/td").text
wattage = table2.find_element_by_xpath("./tr[10]/td").text
cpu.close()
print(core,thread,cache,clock,turbo,socket,wattage)

导入

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

【讨论】:

  • 哇哦,非常感谢!将尝试一下,并使用您发送的代码进行一些探索。再次感谢!
猜你喜欢
  • 1970-01-01
  • 2020-08-29
  • 2021-12-13
  • 2014-08-18
  • 1970-01-01
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多