'How to find element using two link texts that are separated (using Selenium's find element by link text function)
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:
EXAMPLE HTML
Solution 1:[1]
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cruisepandey |



