通过xpath查找元素,其中xpath具有变量

时间:2020-04-14 10:45:52

标签: python selenium xpath

我想从https://www.asx300list.com/中找到“代码”。 “代码”的xpath如下:

ABP

//*[@id="post-2"]/div/table/tbody/tr[1]/td[1]

AX1

//*[@id="post-2"]/div/table/tbody/tr[2]/td[1]

您会看到xpath基于'[tr]'的变化。

我试图创建一个代码来更改每个xpath的变量,但是我不断收到错误消息,说它不是有效的xpath表达式。

from selenium import webdriver
chrome_path = r"C:\Users\Harrison Pollock\Downloads\Python\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get('https://www.asx300list.com/')
for tr in range(1,300):
    codes = driver.find_elements_by_xpath('//*[@id="post 2"]/div/table/tbody/tr[{tr}]/td[1]')
    lencodes = len(codes)
    for i in range(lencodes):
        print(codes[i].text)  
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[@id="post-2"]/div/table/tbody/tr[{tr}]/td[1]' is not a valid XPath expression.

4 个答案:

答案 0 :(得分:0)

要从表中获取代码,请生成WebDriverWait()并等待visibility_of_all_elements_located()并跟随xpath

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from selenium import webdriver
chrome_path = r"C:\Users\Harrison Pollock\Downloads\Python\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get('https://www.asx300list.com/')
elements=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,"//table[@class='tableizer-table sortable']/tbody//tr/td[1]")))
for element in elements:
    print(element.text)

输出

ABP
AX1
ABC
APT
AGL
AQG
ALQ
ALU
AWC
AMA
AYS
AMC
AMP
ANN
APE
APA
ADI
APX
ARB
ALG
ARF
ALL
AHY
ASX
ALX
AUB
AIA
AD8
AMI
AZJ
AST
ASB
ANZ
AAC
API
AVN
AVH
BBN
BOQ
BAP
BPT
BGA
BGL
BEN
BHP
BIN
BKL
BSL
BLD
BXB
BVS
BRG
BKW
BUB
BWP
BWX
CTX
CDD
CVN
CAR
CWP
CNI
CIP
COF
CGF
CIA
CHC
CLW
CQR
CQE
CNU
CIM
CWY
CUV
CCL
COH
CDA
COL
CKF
CBA
CPU
COE
CTD
CGC
CCP
CMW
CWN
CSL
CSR
DCN
DTL
DXS
DHG
DMP
DOW
ECX
ELD
EHL
EML
EHE
A2M
EVN
FAR
FBU
FLT
FMG
FNP
FPH
FXL
GDI
GEM
GMA
GMG
GNC
GOR
GOZ
GPT
GUD
GWA
GXY
HLS
HPI
HSN
HT1
HUB
HVN
IAG
IAP
IEL
IFL
IFM
IFN
IGL
IGO
ILU
IMD
IMF
INA
ING
INR
IPH
IPL
IRE
IRI
ISX
ITG
IVC
JBH
JHC
JHG
JHX
JIN
JMS
KAR
KGN
LIC
LLC
LNK
LOV
LYC
MAH
MFG
MGR
MGX
MIN
MLD
MMS
MND
MNY
MP1
MPL
MQG
MSB
MTS
MVF
MYR
MYS
MYX
NAB
NAN
NCK
NCM
NCZ
NEA
NEC
NGI
NHC
NHF
NIC
NSR
NST
NUF
NWH
NWL
NWS
NXT
OFX
OGC
OML
ORA
ORE
ORG
ORI
OSH
OZL
PDL
PDN
PET
PGH
PLS
PME
PMV
PNI
PNV
PPT
PRN
PRU
PTM
QAN
QBE
QUB
RDC
REA
REG
RFF
RHC
RIO
RMD
RMS
RRL
RSG
RWC
S32
SAR
SBM
SCG
SCP
SDA
SDF
SEK
SFR
SGF
SGM
SGP
SGR
SHL
SHV
SIG
SIQ
SKC
SKI
SLC
SLR
SM1
SOL
SPK
SPL
SSM
STO
SUL
SUN
SVW
SWM
SXL
SXY
SYD
SYR
TAH
TCL
TGR
TLS
TNE
TPM
TWE
UMG
URW
VCX
VEA
VOC
VRL
VRT
VUK
VVR
WAF
WBC
WEB
WES
WGX
WHC
WOR
WOW
WPL
WPP
WSA
WTC
XRO
Z1P

Process finished with exit code 0

答案 1 :(得分:0)

我相信这是一个带有字符串连接的python问题。试试

codes = driver.find_elements_by_xpath('//*[@id="post 2"]/div/table/tbody/tr[' + str(tr) + ']/td[1]')

您的最终xpath仍应在数字的{tr}位置,检查此帮助以了解字符串串联:https://www.jquery-az.com/5-ways-python-string-concatenation-6-examples/

因为您的tr变量是整数,所以还需要将其强制转换为字符串str(tr)

顺便说一句。我想你可以使用一个更简单的xpath //*[@id="post 2"]//tr[' + str(tr) + ']/td[1]')

答案 2 :(得分:0)

尝试以下方法:

wait = WebDriverWait(driver, 20)
Codes=wait.until(EC.presence_of_all_elements_located((By.XPATH, "//table[@class='tableizer-table sortable']//tbody//tr[*]//td[1]")))
 for code in Codes:
       print code.text

注意::请在您的解决方案中添加以下内容

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

答案 3 :(得分:0)

省略tr规范[1]或[2]会同时给出两者,因此您可以使用

//*[@id="post-2"]/div/table/tbody/tr/td[1]