我通过ssh和php shell_exec调用相同的python脚本并获得不同的结果。我正在尝试根据用户输入下载文件。在ssh和php中,脚本运行并最终在下载URL上,并单击下载按钮。但是,php版本没有指定目录中的文件。
PHP
shell_exec("python /home/ubuntu/selenium_crawler.py [three private parameters]");
SSH
"python /home/ubuntu/selenium_crawler.py [three private parameters]"
Python文件
import sys
import re
import time
activate_this_file = "/home/ubuntu/env/bin/activate_this.py"
execfile(activate_this_file, dict(__file__=activate_this_file))
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.common import action_chains, keys
from selenium.webdriver.chrome.options import Options
def selenium_crawler(argv):
""" Crawler to login to an account and grab some urls """
if not len(argv[0]) > 0 and not len(argv[1]) > 0:
return "Missing argument, use format: python selenium_crawler.py username password";
username = argv[0]
password = argv[1]
page_url = argv[2]
login_url = "URL HERE"
# Create browser
display = Display(visible=0, size=(800, 600))
display.start()
options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", {
"download.default_directory": "/home/ubuntu/tmp/",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
driver = webdriver.Chrome(chrome_options=options)
login(driver, username, password, login_url)
response = navigate(driver, page_url)
get_link(driver)
def login(driver, username="", password="", login_url=""):
""" Login using Selenium """
# Open login page
driver.get(login_url)
print "Navigated to page %s" % driver.title
driver.find_element_by_id("Username").send_keys(username)
driver.find_element_by_id("Password").send_keys(password)
driver.find_element_by_class_name("btn-save").click()
print "Login was attempted"
def navigate(driver, url):
""" Navigate to the given url """
driver.get(url)
print "Navigating to %s" % url
print driver.current_url
def get_link(driver):
driver.find_element_by_xpath("removed for security reasons").click()
driver.find_element_by_xpath("removed for security reasons").click()
time.sleep(10)
print driver.current_url
driver.find_elements_by_xpath("removed for security reasons")[0].click()
time.sleep(3)
print "Clicked download btn"
print driver.current_url
if __name__ == "__main__":
selenium_crawler(sys.argv[1:])
有什么想法吗?