Search for element based on text using Selenium webdriver in Python - python

I'm trying to create a program that clicks on boxes that contain a certain word, however all of the boxes have other words around them.
For example the site has a bunch of recipes, however I just want the ones that contain the word "soup". So it needs to be able to click on text that say, "tomato soup, "yummy soup", "some other soup type soup", and so on.
I've found this line.
WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.XPATH, "//span[text()='Soup']"))).click()
which is great but only works if you put the exact text in it.
Ex. WebDriverWait(driver, 10).until(expected_conditions.element_to_be_clickable((By.XPATH, "//span[text()='Tomato Soup']"))).click()
If anyone knows how to do a more loose find that would be a big help. Thank You.

If you are using XPath 2.0 you could use a regular expression so look for everything that contains Soup.
//*[matches(#id, '.*Soup.*')]
Maybe take a look at How to use regex in XPath "contains" function.
Update
browser = webdriver.Chrome()
browser.get("https://www.allrecipes.com/search/results/?wt=Soup&sort=re&page=10")
elems = browser.find_elements_by_xpath("//span[contains(text(), 'Soup')]")
for elem in elems[:2]:
print(elem.text)

Related

Find all elements with href tag containing certain text with Selenium and Python

Lets say I have html code with three links
Whatever
Whatever
Whatever
and I want to use selenium to find all elements which have a href tag which includes the string "hey" (in this case the first two links). How would I write python Selenium code which accomplishes this?
This works:
all_href = driver.find_elements(By.XPATH, "//*[contains(#href, 'hey')]")
print(len(all_href)
This XPath will do this work:
"//a[contains(#href,'hey')]"
To use that with Selenium you will need a find_elements or findElements method, depends on the language binding you use with Selenium.
For Selenium in Python this will give you the list of all such elements:
all_hey_elements = driver.find_elements(By.XPATH, "//a[contains(#href, 'hey')]")

Selenium find element by text with &#65279?

I'm pretty new to test automation so how I can click some a link if I know only partial text.
I know that there is find_element_by_partial_link_text but there are words in the code that appear randomly . I am not able to click by partial text if it appears , for instance:
I know only word Example
<a class="name-link">First Example</a>
I am unable click by class because there are a lot of the same class.
So is there any way to ignore ?
I am writing in python.
Try to click() the element through :
Partial Link Text
driver.find_element_by_partial_link_text("First Exa").click()
XPath
driver.find_element_by_xpath("//a[#class='name-link' and contains(.,'First Exa')]").click()
If you want to match link by word "Example" you can try to use search by XPath
driver.find_element_by_xpath('//a[contains(., "Exa") and contains(., "mple")]').click()
But note that it also will match, for example, link "Examine the sample"
If this not an option for you, you might need to use more complex xpath:
//a[starts-with(substring-after(., " "), "Exa") and substring(substring-after(., " "), 5) = "mple"]
to match string in format "(string)(space)Exa(extra non-ascii character)mple"
Also note that method find_by_link_text()/find_by_partial_link_text() searches for text as it appears on page, but not as it appears in HTML source code, so you can just copy text from page rendered by browser and use it as argument for driver.find_element_by_partial_link_text()

Selenium CSS_Selector looking for an element with specific text is turning out a 'NoSuchElementException'

I'm having an issue with looking for an element with a particular text attribute utilizing CSS_Selectors in Selenium. Here is the current line of code I have:
element = driver.find_element(By.CSS_SELECTOR, "li.adTypeItem[text='CLASS']")
I've had trouble using the attribute selector brackets in CSS_Selectors in the past, and clearing this up would really go a long way to better understanding how to use CSS_Selectors in the future.
Please note - i'm not looking for a element with a class, but rather the actual text that is displayed with that element.
Is this the only place on the page with the text "class" displayed? If so you can try:
driver.find_element(By.LINK_TEXT('CLASS'));
driver.find_element(By.PARTIAL_LINK_TEXT('CLASS'));

how to use Selenium to click something

I have two simple questions relevant to Selenium. Actually I am new to this framework.
The questions are raised for:
HERE ARE CHINESE CHARACTERS
and
HERE ARE ANOTHER CHINESE CHARACTERS
How can I use selenium to click each anchor?
Please note that the first has the keyword "title", I know I may use it for match, but no idea how to realize it. I don't plan to use the CHARACTERS presented there, because it varies depending on different projects.
And the second, in this case, the CHINESE CHARACTERS are fixed. Due to there is no other clue, I think I will have to only use it for detection and issue a click event by Selenium.
Please advise, thanks.
You have multiple ways to find both links. Which option to choose depends on the locations of the links on the page, uniqueness of element attributes, text etc.
That said, there are two relevant methods that should be tried first:
find_element_by_link_text()
find_element_by_partial_link_text()
Here is how you should use them:
first_link = driver.find_element_by_link_text(u'HERE ARE CHINESE CHARACTERS')
first_link.click()
second_link = driver.find_element_by_link_text(u'HERE ARE ANOTHER CHINESE CHARACTERS')
second_link.click()
where driver is a Webdriver instance, e.g.:
from selenium import webdriver
driver = webdriver.Firefox()
If you cannot rely on the link texts, then check title and onclick attributes and use one of the following methods:
find_element_by_xpath()
find_element_by_css_selector()
Example (using the first method of two):
first_link = driver.find_element_by_xpath('//a[#title="title_text"]')
first_link.click()
second_link = driver.find_element_by_xpath('//a[#onclick="anotherjsfunc();"]')
second_link.click()
You can use cssSelector in both cases.
css for first a tag should look like
[title='title_text']
and 2nd a tag
[onclick='anotherjsfunc();']

Find text and elements with python and selenium?

When I go to a certain webpage I am trying to find a certain element and piece of text:
<span class="Bold Orange Large">0</span>
This didn't work: (It gave an error of compound class names or something...)
elem = browser.find_elements_by_class_name("Bold Orange Large")
So I tried this: (but I'm not sure it worked because I don't really understand the right way to do css selectors in selenium...)
elem = browser.find_elements_by_css_selector("span[class='Bold Orange Large']")
Once I find the span element, I want to find the number that is inside...
num = elem.(what to put here??)
Any help with css selectors, class names, and finding element text would be great!!
Thanks.
EDIT:
My other problem is that there are multiple of those exact span elements but with different numbers inside..how can I deal with that?
you're correct in your usage of css selectors! Also your first attempt was failing because there are spaces in the class name and selenium does not seem to be able to find standalone identifiers with spacing at all. I think that is a bad development practice to begin with, so its not your problem. Selenium itself does not include an html editor, because its already been done before.
Try looking here: How to find/replace text in html while preserving html tags/structure.
Also this one is relevant and popular as well: RegEx match open tags except XHTML self-contained tags

Categories

Resources