问题描述
Selenium 非常新,事实上这是我在 VBA 中使用的第一个辅助库。
我正在使用适用于 Microsoft Edge 的 Web 驱动程序,但无法弄清楚如何使用发送键,主要是选择页面的整个内容,然后将它们复制到剪贴板。
这是我所拥有的
'''
Sub Send_Keys_CTRL_A_Ctrl_C()
Dim obj As New WebDriver
obj.Start "edge",""
obj.Get "http://www.google.com"
Application.Wait (Now + TimeValue("0:00:01"))
obj.FindElementByClass("body").SendKeys (Keys.Control + "a" +
Keys.Control)
End Sub
'''
对于一些最有可能的人来说很明显,它在最后一行出现错误,老实说,这正是我从 IE 驱动程序中复制的内容,因此它不会继续存在也就不足为奇了。
谢谢。
解决方法
需要 pip install undetected-chromedriver
,但也只能在普通 selenium 和 edgedriver 中工作。
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import undetected_chromedriver as uc
options = uc.ChromeOptions()
options.headless = False
driver = uc.Chrome(options=options)
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'body'))).send_keys(Keys.CONTROL,'a') #press ctrl + a
WebDriverWait(driver,'c') #press ctrl + c
或者,如果您想要页面的全部内容,也可以只执行 print(driver.page_source)
。
我不会 VBA,但这就是它在 Python 中的工作方式。