Mikee
Mikee

Reputation: 854

Selenium locating elements by more than one method

I'm using Selenium to locate elements in a page. Is there any way to combine two methods together? Example:

Method 1: driver.find_elements_by_partial_link_text('html')

Method 2: driver.find_elements_by_class_name('iUh30')

I will ideally like a method that finds elements that has both the partial link text and class name specified.

Upvotes: 0

Views: 1036

Answers (2)

Waqar Nadir
Waqar Nadir

Reputation: 388

If you want to capture all class containing iUh30 use following xpath

//*[contains(@class,'iUh30')] | //*[text()[contains(.,'html')]]

else if you want only elements that have exact text iUh30 in classes

//*[@class='iUh30'] | //*[text()[contains(.,'html')]]

Upvotes: 0

Fenio
Fenio

Reputation: 3625

You can use xpath to combine both selectors:

driver.find_elements_by_xpath("//*[@class='iUh30'][text()[contains(.,'html')]]")

The //* looks for any element with any tag. Might be <a>, might be <div>, <input>, anything. You can just change it to the desired tag.

The above find the element by exact class name. You can also use [contains(@class, 'partial_class')] to find elements by partial class.

The [text()[contains(.,'html')]] looks for elements which partial text is "html"

Upvotes: 1

Related Questions