首页 > 解决方案 > 如何解决来自 Xpath 的此错误?

问题描述

使用下面的代码,我想使用 xpath 提取黄金价格,然后使用线性回归进行基本预测。

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from sklearn.linear_model import LinearRegression
import time
import numpy as np
from sklearn.svm import SVR
import pytz
from datetime import datetime
from sys import argv
import os, psutil

################################################
if len(argv) != 5:
  print (argv[0] + '<train count> <timeout(s)> <predict date(Y/M/D)> <predict clock(H:M:S)>')
  sys.exit(2)
X_predict = [(int(datetime.strptime(argv[3] + " " + argv[4], '%Y/%m/%d %H:%M:%S').timestamp()*(10000000)))]
################################################

X=[]
y=[]
#driver = webdriver.Chrome()
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://goldprice.org/live-gold-price.html')

elem_xpath = '//[@id="gpxtickerLeft_price"]'

for i in range(1, int(argv[1])):
    try:
        elem = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, elem_xpath)))
        print ("train => ", i)
        X.append(int(time.time()*(10000000)))
        y.append(int(elem.text.replace(',', '')))
        time.sleep(int(argv[2]))
    finally:
        driver.quit
        
##############################################
X = np.array(X).reshape(-1, 1)
y = np.array(y).reshape(-1, 1)
X_predict = np.array(X_predict).reshape(-1, 1)
##############################################
    
svr_rbf = LinearRegression()
y_rbf = svr_rbf.fit(X,y).predict(X_predict)

##########################################
#print ('X:'.format(X))
#print ('y:'.format(y))
#print ('X_predict:{}'.format(X_predict))
##########################################

print ('y_rbf: {}'.format(int(y_rbf)))
print('memory usage: {} MB'.format(
int(psutil.Process(os.getpid()).memory_info().rss/1024/1024)
)) 


但执行脚本后,出现以下错误:


C:\Users\Lev\Desktop>python mls.py 6 3 2020/12/11 12:43:06


[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 90.0.4430
[WDM] - Get LATEST driver version for 90.0.4430
[WDM] - Driver [C:\Users\Lev\.wdm\drivers\chromedriver\win32\90.0.4430.24\chrome
driver.exe] found in cache

DevTools listening on ws://127.0.0.1:6275/devtools/browser/10d6bc25-3034-4ca7-a4
37-c0cf39c86274
[4412:5028:0514/123522.805:ERROR:device_event_log_impl.cc(214)] [12:35:22.805] F
IDO: webauthn_api.cc:54 Windows WebAuthn API failed to load
[5524:4228:0514/123532.459:ERROR:ssl_client_socket_impl.cc(947)] handshake faile
d; returned -1, SSL error code 1, net_error -100
[5524:4228:0514/123533.786:ERROR:ssl_client_socket_impl.cc(947)] handshake faile
d; returned -1, SSL error code 1, net_error -100
[5524:4228:0514/123538.624:ERROR:ssl_client_socket_impl.cc(947)] handshake faile
d; returned -1, SSL error code 1, net_error -100
[5524:4228:0514/123538.825:ERROR:ssl_client_socket_impl.cc(947)] handshake faile
d; returned -1, SSL error code 1, net_error -100
Traceback (most recent call last):
  File "mls.py", line 32, in <module>
    elem = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.
XPATH, elem_xpath)))
  File "D:\Python38\lib\site-packages\selenium\webdriver\support\wait.py", line
80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

“[5524:4228:0514/123533.786:ERROR:ssl_client_socket_impl.cc(947)] 握手失败 d;返回 -1,SSL 错误代码 1,net_error -100”这一行只是不断收到垃圾邮件。

我猜Xpatch是错误的。

标签: pythonseleniumxpath

解决方案


XPath 应该是

//span[@id="gpxtickerLeft_price"]

你用过:

//[@id="gpxtickerLeft_price"]

带 [] 的部分称为谓词。有关示例,请参见此页面

它需要一个节点或属性来过滤。// 不是节点。

节点示例:

//div
//*
//text()
//@id

推荐阅读