Fist of all here is the picture of the HTML code:
This is the element I tried to access:
I tried: driver.find_element_by_class_name("btn_green_white_innerfade btn_medium market_commodity_buy_button")
... but it threw up an error.
I would be glad for any help!
Regards
-Eirik
In Selenium, class name do not have support for spaces. Please remove spaces and put . instead to make a css_selector.
Instead of
driver.find_element_by_class_name("btn_green_white_innerfade btn_medium market_commodity_buy_button")
do this :
driver.find_element_by_css_selector("a.btn_green_white_innerfade.btn_medium.market_commodity_buy_button")
Compound class names are not permitted in Selenium. Locates elements whose class name contains the search value. i.e. you won't be able to pass multiple class names. If you pass multiple class names then you will get compound class name exception.
Wrong way:
By.className("alert alert-class")
Right way:
By.className("alert.alert-class")
Related
I want to read out the text in this html element using selenium with python. I just can't find a way to find or select it without using the text (i don't want that because its content changes)
<div font-size="14px" color="text" class="sc-gtsrHT jFEWVt">0.101 ONE</div>
Do you have an idea how i could select it? The conventional ways listed in the documentation seem to not work for me. To be honest i'm not very good with html what doesn't make things any easier.
Thank you in advance
Try this :
element = browser.find_element_by_class_name('sc-gtsrHT jFEWVt').text
Or use a loop if you have several elements :
elements = browser.find_elements_by_class_name('sc-gtsrHT jFEWVt')
for e in elements:
print(e.text)
print(browser.find_element_by_xpath("//*[#class='sc-gtsrHT jFEWVt']").text)
You could simply grab it by class name. It's 2 class names so it would be like so. by_class_name only uses one.
If the class name isn't dynamic otherwise you'd have to right click and copy the xpath or find a unique identiftier.
Find by XPath as long as font size and color attribute are consistent. Be like,
//div[#font-size='14px' and #color='text' and starts-with(#class,'sc-')]
I guess the class name is random?
As the question implies I'm scraping a webpage that has a class name with an underscore in it, I'm unable to locate it. The element is as follows
<span class="s-item__time-left">30m</span> == $0
I've tried finding it by class name
time = driver.find_elements_class_name("s-item__time-left")
This just returns nothing, so I moved onto css selectors
time = driver.find_element_by_css_selector("s-item__time-left")
I tried a variety of the above, with one "." replaceing the 2 underscores and with 2 dots replacing the underscores. Both of these returned nothing as well.
There's no unique ID I can use, they vary constantly, all of the parent classes also use multiple underscores so I can't path to it hoping through the child elements.
I appreciate any suggestions!
Your css selector was wrong. it should be .classname
Try now.
time =driver.find_element_by_css_selector(".s-item__time-left")
print(time.text)
Try using XPATH: driver.find_element_by_xpath('//span[#class="s-item__time-left"]')
I want to get all class named = "panel-content" only, so i have done this:
driver.find_elements_by_css_selector("div.panel-content")
but it also selects the class named = "accordion-table panel-content", as it has "panel-content" string in it's name.
but what i want is only "panel-content" class.how to do that?
https://www.w3schools.com/cssref/css_selectors.asp
Here you go:
CSS selector:
[class='panel-content']
Locator:
driver.find_elements_by_css_selector("[class='panel-content']")
I've tried to target a.nostyle in my code, however when I do so, it will sometimes grab the email above as they share the same tags. I can't seem to find any tags unique to the phone number. How would you go about doing so?
SEE IMAGE BELOW. Any help would be greatly appreciated.
You can try
a.nostyle:not([itemprop])
UPDATE
As it seem that BeautifulSoup doesn't support :not() syntax, you can try workaround
link = [link for link in soup.select('a.nostyle') if 'itemprop' not in link.attrs][0]
to select link with required class attribute which doesn't contain itemprop attribute (as email link)
You can make a list which contains all the "a" tags. Then you can target required tag by using index numbers
Example
allATagContainer = soup.findAll("a")
then you can use allATagContainer[1] to target second a tag.
I am using Selenium webdriver in Python for a web-scraping project.
The webpage, I am working on has a number of Table entries with the same class name.
<table class="table1 text print">
I am using find_element_by_class_name. However I am getting a error :
*Compound class names not permitted *
Another question:
How to iterate through all the tables having the same css classname ?
Thanks
You should use find_elements_by_class_name. This will return an iterable object.
The error you describe happens when you provide multiple class names rather than a single one. An easy way around this is to get the elements using CSSSelector or XPath. Alternatively, you could use find_elements_by_class_name and provide one class name, then iterate through that list to find the elements that match the additional class names you want to match on as well.