如何使用python硒关闭另一个Chrome弹出窗口 [英] How to close yet another chrome popup using python selenium
本文介绍了如何使用python硒关闭另一个Chrome弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
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)
我尝试了这段代码,但没有帮助
i tried this code, but it didn't helps
driver.switchTo().alert().accept();
此代码也不起作用
driver.send_keys("Enter")
如果我右键单击弹出窗口,则根本没有任何操作,因此我找不到XPath,类ID
if i right-click on popup there is simply no action, so i cant find XPath, class id
这是(autoview)扩展名的来源,我认为代码生成此弹出窗口
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硒关闭另一个Chrome弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文