简体   繁体   中英

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM