How can I click contains this? - python

I have following HTML:
<pre>
<div class="selected_text">
<span class="memo">
<span class="title">01.</span>I want This
</span>
<span class="blank"></span>
<span class="price">
100
</span>
</div>
<pre>
I want to find the string that contains text I want this and click it.
So I try to use
driver.find_element_by_xpath('//span[contains(text(),"want")]').click()
But It's not working
How can i perform click on this element ?

Seems there is an issue with your xpath. Use . instead of text() in contains method. Refer this answer to identify difference between them.
Use explicit wait to avoid unnecessary timeout related things.
Refer below code :
WebDriverWait(driver, 45).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(.,'I want This')]"))).click()

Related

How to combine find_all() and find_next() in BeautifulSoup?

So I have such pieces of HTML that I'm trying to parse. What I want to grab is the price ("84.00 USD"):
<div class="HeaderAndValues_headerDetailSection__3c2SZ ProductCatalog_price__25i2r">
<div class="HeaderAndValues_header__3dB61">Wholesale</div>
<span class="notranslate">
<div class="">84.00 USD</div>
</span>
</div>
soup.find(text="Wholesale").find_next().text gives me exactly what I need but only for the first search result. Is there anyway I could combine find_all() and find_next()? smth like soup.find_all(text="Wholesale").find_next() that would grab next text for each found "Wholesale"
Ok, I've found it! Someone might still find it useful
[x.find_next().text for x in page.find_all(text = "Wholesale")]

Find element by CSS selector AFTER certain element with Selenium

I'm looking to get the text "Interesting" which is the first occurrence of the class b after h1.important.
How would I do that in Selenium?
<div class="a">
<div class="b">Not interesting</div>
</div>
<div class="title">
<h1 class="important">Title</h1>
</div>
<div class="a">
<div class="b">Interesting</div>
</div>
Is there a way to find "Interesting" using a fancy selector or xpath?
This would also match the first element: driver.find_elements_by_css_selector(".b").text
driver.find_elements_by_css_selector(".b")
this will return a list in Python-Selenium bindings. so you cannot do .text on it.
Instead try to use driver.find_element like below :
driver.find_element_by_css_selector("div.title+div>.b").text
in case you want to use xpath, try this :
driver.find_element_by_xpath("//div[#class='title']/following-sibling::div/div").text
Note that, CSS_SELECTOR is preferred over xpath in Selenium automation.
This XPath
//h1[#class='important']/../following-sibling::*//*[#class='b']
Should give you the next b class occurrence after the h1.important node as you asking
This xpath should work
(//h1[#class="important"]/following::*[#class='b'])[1]

Python Selenium get element by CSS matching text

Hi I can't figure out how to get an element by CSS and matching with text. I know it can be done with Xpath but I'd rather use CSS.
<div class="button-face">
<div class="button-face-caption"> Text I want to find 1</div>
</div>
<div class="button-face">
<div class="button-face-caption"> Text I want to find 2</div>
</div>
So in by CSS would be something like...
driver.find_element_by_css('div.button-face-caption')
But how can add the text matching to that? i tried with contains and innerText and none seem to work.
As you said it's supported in xpath:
This would be a solution with an xpath using contains and text()
driver.find_element_by_xpath('//div[#class="button-face-caption" and contains(text(),"Text I want to find")]')
The xpath being:
//div[#class="button-face-caption" and contains(text(),"Text I want to find")]
For css, look here: https://sqa.stackexchange.com/q/362/34209 which should allow us to use:
div:contains('Text I want to find')
Which would lead us to
driver.find_element_by_css("div:contains('Text I want to find')")
However this comes with a BIG caveat:
:contains() is not part of the current CSS3 specification so it will
not work on all browsers, only ones that implemented it before it was
pulled. (see w3.org/TR/css3-selectors)
As workaround you can create your own function
def find_by_css(selector, text=''):
return [element for element in driver.find_elements_by_css_selector(selector) if text in element.text][0]
Then you can call it as
find_by_css('div.button-face-caption') # search only by CSS-selector
or
find_by_css('div.button-face-caption', 'Text I want to find 2') # search by CSS + text
As per the following discussions:
CSS selector :contains doesn't work with Selenium
css pseudo-class :contains() no longer allows anchors
The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver).
Solution
You need to consider the ancestor of the <div class="button-face"> element and traverse down. Let us assume that both the <div class="button-face"> are with in a parent <div class="class">:
<div class="class">
<div class="button-face">
<div class="button-face-caption"> Text I want to find 1</div>
</div>
<div class="button-face">
<div class="button-face-caption"> Text I want to find 2</>
</div>
</div>
So to identify the element with text as:
Text I want to find 1:
div.class div:first-child > div.button-face-caption
Text I want to find 2:
div.class div:nth-child(2) > div.button-face-caption
References
You can find a couple of relevant detailed discussions in:
selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”
Finding link using text in CSS Selector is not working

Selenium-Python: Class containing link-text

I am using Python & Selenium to scrap the content of a certain webpage. Currently, I have the following problem: There are multiple div-classes with the same name, but each div-class has different content. I only need the information for one particular div-class. In the following example, I would need the information in the first "show_result"-class since there is the "Important-Element" within the link text:
<div class="show_result">
<a href="?submitaction=showMoreid=77" title="Go-here">
<span class="new">Important-Element</span></a>
Other text, links, etc within the class...
</div>
<div class="show_result">
<a href="?submitaction=showMoreid=78" title="Go-here">
<span class="new">Not-Important-Element</span></a>
Other text, links, etc within the class...
</div>
<div class="show_result">
<a href="?submitaction=showMoreid=79" title="Go-here">
<span class="new">Not-Important-Element</span></a>
Other text, links, etc within the class...
</div>
With the following code I can get the "Important-Element" and its link:
driver.find_element_by_partial_link_text('Important-Element'). However, I also need the other information within the same div-class "show-result". How can I refer to the entire div-class that contains the Important-Element in the link text? driver.find_elements_by_class_name('show_result') does not work since I do not know in which of the div-classes the Important-Element is located.
Thanks,
Finn
Edit / Update: Ups, I found the solution on my own using xpath:
driver.find_element_by_xpath("//div[contains(#class, 'show_result') and contains(., 'Important-Element')]")
I know you've found an answer but I believe it's wrong since you would also select the other nodes because Important-Element is still in Non-Important-Element.
Maybe it works for your specific case since that's not really the text you're after. But here are a few more answers:
//div[#class='show_result' and starts-with(.,'Important-Element')]
//div[span[text()='Important-Element']]
//div[contains(span/text(),'Important-Element') and not(contains(span/text(),'Non'))]
There are more ways to write this...
Ups, i found the solution on my own via xpath:
driver.find_element_by_xpath("//div[contains(#class, 'show_result') and contains(., 'Important-Element')]")

Selenium, select a particular span element

I'm trying to use selenium with python to make some tests. I'm having trouble in selecting an element. this element makes is a part of a drop-down list and it looks like this:
<li data-original-index="16">
<a tabindex="0" class="" data-normalized-text="<span class='text'>Porto</span>">
<li data-original-index="17">
<a tabindex="0" class="" data-normalized-text="<span class='text'>Santarem</span>">
And so on. I want to select the one with the span text "Porto".
I tried the following, but with no success:
driver.find_element_by_xpath("//span[text()="Porto"]")
Any idea on how can I do this?
Try
driver.find_element_by_link_text("Porto");
Based on the HTML you linked it seems like it might be:
driver.find_element_by_xpath("//a[#data-normalized-text="<span class='text'>Porto</span>"]")
I could be of more help if you posted all the HTML.

Categories

Resources