find_element_by_class_name class spaces does not work - python

I'm trying to use find_element_by_class_name where the class has spaces and it does not work:
Here is the code:
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="/user/santanderbrasil">Santander Brasil</a>
I want the get the content "Santander Brasil"
I tried the following:
driver.find_element_by_class_name("yt-simple-endpoint.style-scope.yt-formatted-string")
driver.find_element_by_class_name("a.yt-simple-endpoint.style-scope.yt-formatted-string")
and
driver.find_element_by_class_name("a.yt-simple-endpoint.")
none of the worked...
Any help?

Use the css selector function instead.
driver.find_element_by_css_selector("a.yt-simple-endpoint.style-scope.yt-formatted-string")

Those are three separated classes, find_element_by_class_name() receives only one class as parameter. For example
driver.find_element_by_class_name('yt-simple-endpoint')
The . you added between the classes represent class in css_selector. You can use it to locate the element by all three classes use css_selector
driver.find_element_by_css_selector('.yt-simple-endpoint.style-scope.yt-formatted-string')
Or by xpath
driver.find_element_by_xpath('//a[#class="yt-simple-endpoint style-scope yt-formatted-string"]')

Try this
driver.find_element_by_xpath("//div[contains(#class, 'yt-simple-endpoint') and contains(#class, 'style-scope') and contains(#class, 'yt-formatted-string')]"")
Not sure about the find_element_by_xpath method because i don't use selenium in python, but whatever that function is, selector should do the trick.
Hope this helps.

All of these work for me (Firefox driver):
yt-simple-endpoint
style-scope
yt-formatted-string
yt-simple-endpoint.style-scope.yt-formatted-string
Note that the last class name is actually the same as the first one in your question. It works because Selenium internally converts the class name into a CSS selector and then uses it for the search. If you want to nail things down for specific tags, e.g. only match <a> tags with those classes then you will need to look at CSS selectors and other options such as XPath.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("file:///tmp/test.html")
for class_name in 'yt-simple-endpoint', 'yt-formatted-string', 'style-scope', 'yt-simple-endpoint.style-scope.yt-formatted-string':
e = driver.find_element_by_class_name(class_name)
print(e.text)
driver.close()
Output
Santander Brasil
Santander Brasil
Santander Brasil
Santander Brasil
The test.html file contains:
<html>
<head><title>Test</title></head>
<body>
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="/user/santanderbrasil">Santander Brasil</a>
</body>
</html>

Related

partial text in xpath

<a class="ui-autocomplete-row ui-corner-all"
aria-label="(1100)Texas (PL1200/PC1030)- (200167)-Supplies: Other Supplies (620038): 1100-1200-200167-620038"
id="ui-id-7" tabindex="-1">
(1100)- (PL1200/PC1030)-(200167)-Supplies:
Other Supplies (620038):
<u><b>1100-1200-200167-620038</b></u>
</a>
In Xpath, I want to check if aria-label contains the value "1100-1200-200167-620038". It looks like there is a syntax error in the below statement:
element = wait.until(EC.visibility_of_element_located((By.XPATH,'//*[#aria-label="1100-1200-200164-620038"]')))
You can use two option like this:
CSS Selector
element = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'a[aria-label*="1100-1200-200167-620038"]')))
Xpath:
element = wait.until(EC.visibility_of_element_located((By.XPATH, '//a[contains(#aria-label, "1100-1200-200167-620038")]')))
Xpath can be a hassle sometimes, I recommend using the CSS selector method to accomplish this:
element = driver.find_elements_by_css_selector("[aria-label='1100-1200-200167-620038']")
You could them use the element in your visibility_of_element_located method.

Get text from div using Selenium and Python

Situation
I'm using Selenium and Python to extract info from a page
Here is the div I want to extract from:
I want to extract the "Registre-se" and the "Login" text.
My code
from selenium import webdriver
url = 'https://www.bet365.com/#/AVR/B146/R^1'
driver = webdriver.Chrome()
driver.get(url.format(q=''))
elements = driver.find_elements_by_class_name('hm-MainHeaderRHSLoggedOutNarrow_Join ')
for e in elements:
print(e.text)
elements = driver.find_elements_by_class_name('hm-MainHeaderRHSLoggedOutNarrow_Login ')
for e in elements:
print(e.text)
Problem
My code don't send any output.
HTML
<div class="hm-MainHeaderRHSLoggedOutNarrow_Join ">Registre-se</div>
<div class="hm-MainHeaderRHSLoggedOutNarrow_Login " style="">Login</div>
By looking this HTML
<div class="hm-MainHeaderRHSLoggedOutNarrow_Join ">Registre-se</div>
<div class="hm-MainHeaderRHSLoggedOutNarrow_Login " style="">Login</div>
and your code, which looks okay to me, except that part you are using find_elements for a single web element.
and by reading this comment
The class name "hm-MainHeaderRHSLoggedOutMed_Login " only appear in
the inspect of the website, but not in the page source. What it's
supposed to do now?
It is clear that the element is in either iframe or shadow root.
Cause page_source does not look for iframe.
Please check if it is in iframe, then you'd have to switch to iframe first and then you can use the code that you have.
switch it like this :
driver.switch_to.frame(driver.find_element_by_xpath('xpath here'))

How to identify an element using webdriver in python for http link

How to identify the link, I have inspected the elements which are as below :
<div class="vmKOT" role="navigation">
<a class="Ml68il" href="https://www.google.com" aria-label="Search" data-track-as="Welcome Header Search"></a>
<a class="WaidDw" href="https://mail.google.com" aria-label="Mail" data-track-as="Welcome Header Mail"></a>
<a class="a4KP9d" href="https://maps.google.com" aria-label="Maps" data-track-as="Welcome Header Maps"></a>
<a class="QJOPee" href="https://www.youtube.com" aria-label="YouTube" data-track-as="Welcome Header YouTube"></a>
</div>
I want to identify the class WaidDw or href and click it using python.
You can try
driver.find_element_by_class_name('WaidDw').click()
or
driver.find_element_by_xpath('//a[#href="https://mail.google.com" and #aria-label="Mail"]').click()
In your provided HTML all attribute's values are unique, you can locate easily that element by using their attribute value.
As your question points to locate this <a class="WaidDw" href="https://mail.google.com" aria-label="Mail" data-track-as="Welcome Header Mail"></a> element. I'm providing you multiple cssSelectors which can work easily to identify the same element as below :-
a.WaidDw
a.WaidDw[href='https://mail.google.com']
a.WaidDw[aria-label='Mail']
a.WaidDw[data-track-as='Welcome Header Mail']
a.WaidDw[href='https://mail.google.com'][aria-label='Mail']
a.WaidDw[href='https://mail.google.com'][aria-label='Mail'][data-track-as='Welcome Header Mail']
Note :- Keep in practice (priority) to use cssSelector instead xpath if possible, because cssSelectors perform far better than xpath
Locating Element by CSS Selectors using python :-
element = driver.find_element_by_css_selector('use any one of the given above css selector')
Clicks the element :-
element.click()
Reference link :-
https://www.w3schools.com/cssref/css_selectors.asp
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

Is there a way to find part of a css class within markup using Scrapy?

Using Scrapy, is there a way to find a part of a class within the markup of a page, for example, if I have multiple classes such as "name-1, name-2, name-3", how can I find the base only? i.e. "name-"
You can also apply "starts-with" checks in a pure XPath or CSS selector approach:
response.xpath('//*[starts-with(#class, "name-")]')
response.css('[class^="name-"]')
Scrapy selectors accept regular expressions, see http://doc.scrapy.org/en/latest/topics/selectors.html#regular-expressions
from scrapy import Selector
html = """
<a class="name-1" href="#">foo</a>
<a class="name-2" href="#">bar</a>
<a class="name-3" href="#">foo</a>
<a class="name-foo" href="#">teststr</a>
"""
sel = Selector(text=html, type="html")
print sel.xpath('//a[re:test(#class, "name-\d$")]').extract()

Click the link, if we have only class name

How can I click the link:
<a class="single_like_button btn3-wrap" onclick="openFbLWin_189932();">
<span> </span><div class="btn3">Share</div>
</a>
This is my code in Python, but it doesn't work. I use Selenium
......
elem = self.driver.find_elements_by_class_name("single_like_button btn3-wrap")[0].click();
......
find_elements_by_class_name() is apparently limited to a single class name.
You can use find_element_by_css_selector() instead and write:
self.driver.find_element_by_css_selector(
".single_like_button.btn3-wrap").click();

Categories