简体   繁体   English

如何从指定的 XPATH 中找到所有元素,但接受带有 Selenium 的稍微可变的 ID?

[英]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所以我试图让所有元素都像这样,除了 ccid 之后的数字可以是任何东西

//*[@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.基本上,我想要每个 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?所以你有几乎正确的 xpath,你只需要指出你只需要第一次出现的元素?

   //*[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]为了给你准确的答案,我们需要看看 td[12] 的 html 结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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