Find element by CSS selector AFTER certain element with Selenium - python

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]

Related

Python Selenium find element with following sibling by class, id, a (hreff or class)

I need help with finding an exact element and click it with following-sibling based on specific id number and then classes and a (href or class).
Here is simplified code, the below example occurs many times just with different id:
<div class="class_1" id="1234567">
<div class="class_2">
<div class="class_3">
<div class="class_3.1">
<div class="class_3.2">
<div class="class_3.3">
<div class="class_3.3.1">
<div class="class_3.3.1.1">
<div class="class_3.3.1.2">
<div class="class_3.3.1.3">
...
How can I locate an element with id and class for example something like this and click on it:
driver.find_element(By.XPATH, 'class=class_1 and id="2222222" and class="event-media-icon live-icon icon-white').click()
The xpath you are looking for will look like the following:
//div[#class='class_1' and(#id='1234567')]//a[#data-sport='soccer']
I guess the elements between the upper div and the goal a are not important so we can omit them.
The href value looks not unique too so I preferred using data-sport attribute that can be more unique.
To give more precise answer I need to see that web page with dev tools.
This xpath should work fine too
.//div[#class='class_1' and #id='1234567']//following-sibling::a[#data-sport='soccer']

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

How can I click contains this?

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()

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.

How to select a tag by css selector

I'm new in selenium. I'm trying to select one tag by css selector by there is a mistake somewhere.
<li class="arr-r active">
<a class="sem" alt="Alter" href="/something.html" data-page="2"><span>2</span></a>
</li>
I've tried many options but none of it works.
self.driver.find_element_by_css_selector('li.arr-r.active.a').click()
Could you give me a hint where the problem is?
a is not a class; Remove a dot before a element.
self.driver.find_element_by_css_selector('li.arr-r.active a').click()
or
self.driver.find_element_by_css_selector('li.arr-r.active>a').click()
try:
self.driver.find_element_by_css_selector('li.arr-r.active a').click()
"a" is not a class, is a tag. So you should not put a dot before "a".

Categories

Resources