Python Selenium脚本仅在dekstop上起作用,而在我的服务器上不起作用

时间:2019-05-06 22:05:10

标签: python python-3.x

我创建了一个脚本,可以将产品导入到我的woocommerce商店,并且效果很好,但是我尝试将其安装在服务器上(Ubuntu 18.04.2 LTS(GNU / Linux 4.15.0-48-通用x86_64)),但是每个时间到了

File "importer.py", line 54, in <module>
    browser.find_element_by_id("ATKP_lists").send_keys(row, ";EAN;;woo")
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"ATKP_lists"}
  (Session info: headless chrome=74.0.3729.131)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.15.0-48-generic x86_64)

我使用python 3.6.6在我的PC上测试了脚本,并且效果很好。

PC上的脚本:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
import csv
import time

chromedriver = 'chromedriver.exe'
chrome_options = Options()
browser = webdriver.Chrome(chromedriver, chrome_options=chrome_options)


browser.get("https://example.org/cp")
print('example.org aufgerufen')
shop = 'xxx)'
datafile = 'datafeed.txt'
time.sleep(2)
username = browser.find_element_by_id("user_login")
password = browser.find_element_by_id("user_pass")
time.sleep(2)

username.send_keys("xxx")

time.sleep(2)

password.send_keys("xxx")

product = 0
time.sleep(2)
login_attempt = browser.find_element_by_xpath("//*[@type='submit']")

login_attempt.submit()
with open(datafile) as Datafeed:
    csvReader = csv.reader(Datafeed)
    for row in csvReader:
        product = product + 1

        browser.get("https://example.org/productimport")
        time.sleep(5)
        print("Started import of", row, "(NR:", product, ")")
 #       select = Select(browser.find_element_by_id('ATKP_listshop'))
 #       select.select_by_visible_text(shop)

        browser.find_element_by_id("ATKP_lists").send_keys(row, ";EAN;;woo")
        browser.find_element_by_id("saveimportproduct").click()

        print('imported: ', row, 'Nr. ', product)
        with open(datafile, 'r') as fin:
            data = fin.read().splitlines(True)
        with open(datafile, 'w') as fout:
            fout.writelines(data[1:])
        print('Product-Line Deleted')

服务器上的脚本:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC

import csv
import time
chromedriver = '/home/scripts/chromedriver'


chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('user-data-dir=selenium')
browser = webdriver.Chrome(chromedriver, chrome_options=chrome_options)

browser.get("https://example.org/cp")

print('xxx')
shop = 'xxx'
datafile = 'datafeed.txt'
time.sleep(2)
username = browser.find_element_by_id("user_login")
password = browser.find_element_by_id("user_pass")
time.sleep(2)

username.send_keys("xxx")

time.sleep(2)

password.send_keys("xxx")

product = 0
time.sleep(2)
login_attempt = browser.find_element_by_xpath("//*[@type='submit']")

login_attempt.submit()

with open(datafile) as Datafeed:
    csvReader = csv.reader(Datafeed)
    for row in csvReader:
        product = product + 1
        browser.get("https://example.org/productimport")
        print("Started import of", row, "(NR:", product, ")")
#       select = Select(browser.find_element_by_id('ATKP_listshop'))
 #       select.select_by_visible_text(shop)
        time.sleep(4)
        browser.find_element_by_id("ATKP_lists").send_keys(row, ";EAN;;woo")
        browser.find_element_by_id("saveimportproduct").click()

        print('imported: ', row, 'Nr. ', product)
        with open(datafile, 'r') as fin:
            data = fin.read().splitlines(True)
        with open(datafile, 'w') as fout:
            fout.writelines(data[1:])
        print('Product-Line Deleted')





编辑:我认为它无法打开example.org/productimport页面

有人知道该错误吗?或其他方法?

0 个答案:

没有答案