0

I want to find elements using two partial link texts. I want to do this to separate instances where the first link text is the the same as another one.

For example if link text 1 for an element is "Publix Eggs, Large" and link text 2 is "12 ct", and link text 1 for another item is also "Publix Eggs, Large" but its link text 2 is "18ct" I want to be able to find only the first items element if I were to write something like below:

enter image description here

EXAMPLE HTML

link and separated link texts

what i'm trying to avoid grabbing

Nova42
  • 13
  • 1
  • 4
  • Welcome to Stack Overflow. [Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied, or even consumed by users of adaptive technologies like screen readers. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Apr 18 '22 at 17:45

1 Answers1

0

You can do that with xPath.

//a[contains(.,'Publix Eggs, Large') or contains(.,'12ct')]

however, if you are just concerned about fetching the first element, then you can use

find_element

link = driver.find_element(By.PARTIAL_LINK_TEXT, "Publix Eggs, Large")
link.click()

Note that even though there are multiple matching nodes in HTML, it will always pick the first one.

Using find_elements

links = driver.find_elements(By.PARTIAL_LINK_TEXT, "Publix Eggs, Large")
links[0].click()
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • doesn't the "or" in the first solution find the element with one or the other? I want something that finds the element with both "Publix Eggs, Large" and "12 ct" ignoring whether or not it is the first item because I will be for looping it. Because of that the texts being looped need to be unique which is why I want both "Publix Eggs, Large" and "12 ct" because that will make it unique. – Nova42 Apr 18 '22 at 19:02
  • `doesn't the "or" in the first solution find the element with one or the other? ` - it depends on how do you use them? if you would use `find_element` it's gonna return the first matching node irrespective of `or`. If you want that be unique use `and`, it should only look for both the condition `//a[contains(.,'Publix Eggs, Large') and contains(.,'12ct')]` – cruisepandey Apr 19 '22 at 09:21