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
248 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?

So I'm trying to get ALL elements EXACTLY like this with the exception that the numbers after ccid can be anything

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

This code below gets elements almost to my liking but it also pulls up two extra elements and I'm not sure how to code it to get only the one shown above (with the exception i talked about):

classnames = driver.find_elements_by_xpath('//*[starts-with(@id, "ccid")]/td[12]')

The code above (classnames variable) is almost perfect but I don't want it to pull these elements:

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

Basically, I want the first element under each ID. If you need clarification, please tell me. I will do my best to explain.

question from:https://stackoverflow.com/questions/65855697/how-can-i-find-all-elements-from-a-specified-xpath-but-accept-slightly-variable

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

1 Answer

0 votes
by (71.8m points)

So you have almost correct xpath, where you just need to indicate that you need only first occurense of element?

   //*[startsWith(@id, 'ccid_')]/td[12]/text()[1]

You can also try one of these ones, if it suites you:

   //*[startsWith(@id, 'ccid_')]/td[12]/*[1]/text()
   //*[startsWith(@id, 'ccid_')]/td[12]//text()[1]

To give you precise answer, we need to see html structure of td[12]


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

...