【问题标题】:how to open a new window with selenium python如何用 selenium python 打开一个新窗口
【发布时间】:2023-03-25 04:50:01
【问题描述】:

我正在网站上进行自动化。

我在这个网站上搜索,你的结果返回一个链接供我访问,点击它打开一个新标签,但我想打开一个新窗口

这是我点击链接的代码

WebDriverWait(self.browser, timeout=60).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="tabelaResultado"]/div[1]/table/tbody[1]/tr/td[1]/span/a'))).click()

我尝试使用 SHIFT 键盘快捷键

WebDriverWait(self.browser, timeout=60).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="tabelaResultado"]/div[1]/table/tbody[1]/tr/td[1]/span/a'))).send_keys(Keys.SHIFT).click()

但我有一个结果错误

AttributeError: 'NoneType' 对象没有属性 'click'

有没有一种方法可以配置 chrome,以便每次单击链接时都会打开一个新窗口?

from selenium.webdriver.chrome.options import Options

【问题讨论】:

    标签: python selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    我认为您必须通过发送 ShiftEnter 来编写它:

    WebDriverWait(self.browser, timeout=60).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="tabelaResultado"]/div[1]/table/tbody[1]/tr/td[1]/span/a'))).send_keys(Keys.SHIFT,Keys.ENTER)
    

    【讨论】:

      【解决方案2】:

      send_keys() 没有返回值,所以你得到的是None。你可以用ActionChains来做到这一点

      from selenium.webdriver import ActionChains
      
      link = WebDriverWait(self.browser, timeout=60).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="tabelaResultado"]/div[1]/table/tbody[1]/tr/td[1]/span/a')))
      ActionChains(driver).key_down(Keys.SHIFT, link).click().key_up(Keys.SHIFT).perform()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-14
        • 2013-06-23
        • 2020-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多