python - 如何使用 python selenium 关闭另一个 chrome 弹出窗口

标签 python selenium selenium-webdriver web web-scraping

enter image description here

Link to "chrome_options.add_extension(r'C:\Users\Administrator\Desktop\chromedriver_win32\extension.crx')"

这是我的代码:

import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import gmtime, strftime
from os import system
import pyperclip
import datetime
import time
import os
import sys



executable_path = r"C:\Users\Administrator\Desktop\chromedriver_win32\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = executable_path
chrome_options = Options()
chrome_options.add_extension(r'C:\Users\Administrator\Desktop\chromedriver_win32\extension.crx')
driver = webdriver.Chrome(executable_path=executable_path, chrome_options=chrome_options)
time.sleep(4)
driver.get("https://www.tradingview.com/chart/gK6Rq0UH/")
time.sleep(4)
driver.find_element_by_xpath("""//*[@id="footer-chart-panel"]/div[2]/span[4]""").click()
time.sleep(4)
while True:
    driver.find_element_by_xpath("""//*[@id="bottom-area"]/div[2]/div[1]/div[2]/ul/li[4]""").click()
    time.sleep(2)
    contents = pyperclip.paste()
    time.sleep(2)
    filepath = r"C:\Users\Administrator\Desktop\DATA.txt"
    time.sleep(2)
    with open(filepath, 'w') as f: # 'w' means write mode and we get the file object as f
        f.write(contents)
        time.sleep(5)   

我尝试了这段代码,但没有帮助

driver.switchTo().alert().accept();

这段代码也不起作用

driver.send_keys("Enter")

如果我右键单击弹出窗口,则根本没有任何操作,因此我找不到 XPath、类 id

this is sources that (autoview) extension, i assume that this code generate this popup

function clipboard(format, data) {
    const tmp = document.oncopy;

    document.oncopy = function clipboard_oncopy(e) {
        e.clipboardData.setData(format, data);
        e.preventDefault();
    };
    document.execCommand("copy", false, null);
    alert("Copied to Clipboard");

    document.oncopy = tmp;
}

最佳答案

试试这个:

driver.switch_to_alert().accept()

关于python - 如何使用 python selenium 关闭另一个 chrome 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49257433/

相关文章:

python - 从 Postgres 中提取数据,并将其显示在模板中

python - 为什么具有零数据集的 XGBoost 返回非零预测?

c# - Selenium 3.8.0 wait.until 调用抛出异常

firefox - Selenium in -browserSessionReuse 模式启动一个新的浏览器

python - 无法检测到该元素并通过 Selenium 将输入发送到信用卡号码字段

java - getText() 获取任何标签之外的元素,而不在 xpath 中使用 text()

javascript - 通过selenium Python复制粘贴?

python - 如何从生成的 Django 表单中删除文本?

selenium - 通过设置正确的路径获取 "The path to the driver executable must be set by the webdriver.chrome.driver system property"

python - 如何使用 python 中的 raw_input 输入值并将它们添加到字典中的字典中?