from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def main():
# Set the path to the chromedriver
chromedriver_path = "path/to/chromedriver"
# Create ChromeOptions
options = Options()
# Your Options here....
# options.add_argument("--headless") # Run in headless mode
# options.add_argument("--disable-gpu") # Disable GPU acceleration
# Create a ChromeDriver
driver = webdriver.Chrome(executable_path=chromedriver_path, options=options)
# Open Website
driver.get("https://www.google.com")
# your code.. clicking on buttons etc..
# Close the ChromeDriver
driver.quit()
if __name__ == "__main__":
main()
如果使用未检测到的chromedriver:
pip3 install undetected-chromedriver
示例代码:
import undetected_chromedriver as uc
def main():
# Set the path to the chromedriver executable
chromedriver_path = "path/to/chromedriver"
# Create options object
options = uc.ChromeOptions()
# Add options if needed
# options.add_argument("--headless") # Run in headless mode
# options.add_argument("--disable-gpu") # Disable GPU acceleration
# Create an instance of Undetected ChromeDriver with options
driver = uc.Chrome(executable_path=chromedriver_path, options=options)
try:
# Open Google
driver.get("https://www.google.com")
# Rest of your code for interacting with the Google page
except Exception as ex:
print(ex)
finally:
# Close the ChromeDriver
driver.quit()
if __name__ == "__main__":
main()
5条答案
按热度按时间f45qwnt81#
找到chrome Options class in the Selenium source code了。
创建Chrome驱动程序示例的用法:
ecfdbz9o2#
我就是这么做的。
irlmq6kh3#
禁用chrome扩展的代码,使用DesiredCapabilities设置浏览器标志:
4ioopgfo4#
desired_capabilities和选项.to_capabilities()都是字典。您可以使用dict.update()方法将options添加到主集中。
w8f9ii695#
如果您使用的是普通chromedriver:
示例代码:
如果使用未检测到的chromedriver:
示例代码:
以下是我经常使用的一些Chrome选项: