Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

python - How can I find all elements from a specified XPATH but accept slightly variable IDs with Selenium?

Yes, I have looked at many solutions, tutorials, etc. But I am stumped here as I continue to get errors. I want to find every element with this exact XPATH (below) but still accept different numbers/characters after the 'ccid'

//*[@id="ccid_4587719"]/td[12]/text()[1]

#in this case, the numbers/characters I am referring to after the 'ccid' is 4587719.

My best attempt so far is this.

classnames = driver.find_elements_by_xpath('//*[starts-with(@id, "ccid")]/td[12]')
for classname in classnames:
    classinfo = classname.text
    print(classinfo)

'''/text() is excluded from the end of the classnames variable 
as it causes an error for being an [object Text] instead of an element'''

This example above DOES get elements with the ID starting with "ccid" but it ALSO gets elements such as these below which I DON'T want to get.

//*[@id="ccid_4587719"]/td[12]/a[2]
//*[@id="ccid_4587719"]/td[12]/text()[3]

#these ones have different numbers at the end-- [2] and [3] respectively-- which may help get an answer.

Basically, how can I make it so the classnames variable finds elements by the XPATH in the first code block but allow (still get) elements with different numbers after the 'ccid'?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...