我已经成功地构建并测试了一个登录到我的wordpress网站的Selenium脚本(使用python和chromedriver(。该应用程序在我的本地计算机上完美运行。但是,自从将其部署到 Ubuntu 服务器后,我的脚本会触发mod_security规则并阻止我登录我的 wordpress 网站。
这是否与 cookie 或它在服务器上运行的事实有关?
任何帮助将不胜感激。Mod_security错误
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import os
import shutil
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from dashboard.models import Site
import glob
def Backup():
prefs = {
'download.default_directory': r"/home/landon/app/static/dashboard/backups",
# 'download.default_directory': r"/Users/landonroddenberry/Documents/appThree/SITERACK_1.0/dashboard/static/dashboard/backups", #### USE only for local testing and devs #####
'download.prompt_for_download': False,
'download.extensions_to_open': 'xml',
'safebrowsing.enabled': True
}
chrome_path = r"/usr/bin/chromedriver"
# chrome_path = r"/Users/landonroddenberry/Documents/chromedriver" #### USE only for local testing and devs #####
WINDOW_SIZE = "1920,1080"
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs',prefs)
options.add_argument("start-maximized")
options.add_argument("--headless")
# options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument("--window-size=%s" % WINDOW_SIZE)
options.add_argument("--safebrowsing-disable-download-protection")
options.add_argument("safebrowsing-disable-extension-blacklist")
driver = webdriver.Chrome(executable_path=chrome_path, options=options)
logLink = "/wp-login.php"
AdminLink = "/wp-admin/"
page = "export.php/"
p_col = "https://"
sites = Site.objects.filter(active=True)
for site in sites:
print('')
print('now backing up ' + site.webUrl)
MainLink = (p_col + site.webUrl + AdminLink)
loginLink = (p_col + site.webUrl + logLink)
driver.get(loginLink)
time.sleep(5)
try:
driver.find_element_by_xpath('/html/body/div[1]/form/div[3]/a[1]').click()
time.sleep(3)
except:
print("no button to click")
try:
user_name_elem = driver.find_element_by_xpath('//*[@id="user_login"]')
user_name_elem.clear()
time.sleep(2)
user_name_elem.send_keys(site.u_admin)
time.sleep(2)
passworword_elem = driver.find_element_by_xpath('//*[@id="user_pass"]')
time.sleep(2)
passworword_elem.clear()
passworword_elem.send_keys(site.p_admin)
time.sleep(3)
passworword_elem.send_keys(Keys.RETURN)
time.sleep(5)
driver.save_screenshot('Backup_Conflict_' + site.webUrl + '.png') ##### USED FOR DEBUGGING #######
print('done logging-in')
time.sleep(10)
except:
driver.save_screenshot('Backup_Conflict_' + site.webUrl + '.png')
如果您同时对Windows和Linux操作系统使用相同的chromedriver,则十个它将不起作用,Chromedriver对所有操作系统都是分开的。
尝试下载适用于 Linux 操作系统的 chromedriver 。
希望这有帮助。