Selenium Python css selector by class doesn't work - selenium

I have 4 ways to locate some element and want to click on it:
DOM is:
<div class="ui dropdown selection" tabindex="0">
And I locate this element by four ways:
(By.XPATH, "//div[#class='ui dropdown selection']")
(By.CSS_SELECTOR, "[class='ui dropdown selection']")
(By.CSS_SELECTOR, ".ui dropdown selection")
(By.CLASS_NAME, "ui dropdown selection")
I i just clik on element
Way 1 and 2 work, test is ok - and len(element) is 1
Way 3 and 4 don't work: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".ui dropdown selection"} - and len(element) is 0
(Waits don't help, and Way 1, Way 2 don't require waits at all)
Could you tell me why Way 3 and Way 4 failed ?

When using multiple class names for the same tag within a CSS Selector, they must be separated by a dot instead of a space. Here's the correct way to express your third one:
(By.CSS_SELECTOR, ".ui.dropdown.selection")
OR
(By.CSS_SELECTOR, "div.ui.dropdown.selection").
As for the fourth one, you can't use By.CLASS_NAME with multiple class name components. You would have to pick one, but since that probably won't give you a unique selector, you'll be better off using one of the other ways to form a selector.

#Michael
(By.CSS_SELECTOR, ".ui.dropdown.selection")
(By.CSS_SELECTOR, "div.ui.dropdown.selection")
Both don't work - selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
But notice that there is only 1 element because this works:
(By.XPATH, "//div[#class='ui dropdown selection']")
and len(element) = 1

Related

Selenium and LINK_TEXT

I tried to scrapp a cost with a LINK TEXT but my scrapping method can't find the TEXT:
budget = budgets.append((driver.find_element(By.LINK_TEXT, 'Total budget/expenditure:')).text)
The web site is : https://keep.eu/projects/12880/A-la-d-couverte-des-plus-be-EN/
It works with the XPATH but i need to scrap many page like this one and sometimes the To total budget/expenditure and European Union Funding was not exactly at the same place.
The error is that :
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Total budget/expenditure:"}
(Session info: chrome=100.0.4896.127)
I don't know why sometimes I can used the LINK-TEXT and sometimes no.
I tried PARTIAL_LINK_TEXT too but it can't works.
Link Text
A linkText is used to identify the hyperlinks on a web page. It can be determined with the help of an anchor tag <a>.
But your desired element is within a <strong> tag, so By.LINK_TEXT won't work here.
<p>
<strong>Total budget/expenditure: </strong>
" EUR 313 300.00"
</p>
Solution
To locate the element you can use either of the following locator strategies:
Using xpath and contains():
element = driver.find_element(By.XPATH, "//p//strong[contains(., 'Total budget/expenditure:')]")
Using xpath and starts-with():
element = driver.find_element(By.XPATH, "//p//strong[starts-with(., 'Total budget/expenditure:')]")

Choose the correct element from the list of objects with the same className

Quick one, i am trying to avoid using xpath and using css selectors due to performance issues xpath can have so i would like to know the right approach of locating for example "A" in the list
<div class="input-search-suggests" xpath="1">
<div class="input-search-suggests-item">A</div>
<div class="input-search-suggests-item">B</div>
<div class="input-search-suggests-item">C</div>
</div>
Currently i am locating A using xpath / span but it would be indeed sufficient locating all elements and then grabbing A from the list that have same class which is "input-search-suggests-item"
#FindBy(xpath = "//span[contains(text(),'A')]")
CSS_SELECTOR does not have support for direct text what xpath has.
What this means is, for the below xpath
xpath = "//span[contains(text(),'A')]"
based on text A you can not write a css selector.
Instead to locate A using css selector, you can do :
div.input-search-suggests > div.input-search-suggests-item
In Selenium something like this :
#FindBy(cssSelector= "div.input-search-suggests > div.input-search-suggests-item")
Even though it will have 3 matching nodes, but findElement will take the first web element.
Also you may wanna look at nth-child(n)
div.input-search-suggests > nth-child(1)
to make use of index to locate A, B, C
Here is the Reference Link

find specific element for button using selenium for web scraping

I am inspecting one button element from a web page using chrome driver and selenium. And the html code for the particular button is:
<div class="label text-left text-link link-blue text-
uppercase">Financial Statement Analysis <span class="count">(2)</span>
</div>
I have tried different element options like find element by name, xpath, link text etc. But none of them unable to locate the element.
What will be the element to locate the button. ?
try Xpath :
//span[contains(#class,'count') and text() = '(2)']
You can try with this css selector :
div.label.text-left.text-link.link-blue.text-.uppercase
To locate the element with text as Financial Statement Analysis (2) you can use the following solution:
Java Solution:
WebElement elem = driver.findElement(By.xpath("//div[#class='label text-left text-link link-blue text-uppercase'][contains(.,'Financial Statement Analysis')]"));

Selenium WebDriver - Find Element

I've gone through the Selenium Documentation for locating elements, but I can't seem to figure out how to find the element in my code.
Here is my code from my .cshtml:
<a onclick="alter('#key', '#value')" href="#edit" id="#key-display">#value</a>
I am trying to locate and click the #value at the end.
Here is what it looks like when I inspect the value on Chrome:
<a onclick="alter('February 9, 2018', '1.00000')" href="#edit" id="February 9, 2018-display">1.00000 gallons</a>
I am able to locate the element by link text like this:
chromeDriver.FindElementByLinkText("1.00000 gallons").Click();
However, the link text will change constantly and I want to be able to locate it after it changes.
I have tried locating by several ways:
chromeDriver.FindElementByLinkText("#value").Click();
chromeDriver.FindElementByXPath("//a[#id='#key-display']").Click();
chromeDriver.FindElementById("#key-display").Click()
You will have to locate the element by the HTML in the page after it's rendered so the cshtml variable name can't be used. Having said that, you should be able to find a locator that will work. I would start with a CSS selector like
a[href='#edit']
That should work unless you have multiple edit links on the page. If that doesn't work, I would try
a[href='#edit'][id$='-display']
To find the element and invoke click() on the element you can use either of the following Locator Strategies :
xpath (where ID contains -display and href is #edit)
"//a[contains(#id,'-display') and #href='#edit']"
You can be more granular adding the onclick attribute as :
"//a[contains(#id,'-display') and #href='#edit' and starts-with(#onclick,'alter')]"
cssSelector (where ID ends with -display and href is #edit)
"//a[id$='-display'][href='#edit']"
You can be more granular adding the onclick attribute as :
"//a[id$='-display'][href='#edit'][onclick^='alter']"

Click span element using selenium

I would like to know how can I click on this element using selenium:
<span data-val="BB9049_600"> 7.5 </span>
Since there is no class or id with the span element I cant approach it that way.
The xpath is:
//*[#id="buy-block"]/div[1]/div[5]/div[3]/form/div[2]/div[2]/div/div/div/div[2]/div/ul/li[2]/span
You can try to use text content of required element as
//span[normalize-space()="7.5"]
or value of data-val attribute:
//span[#data-val="BB9049_600"]
you can use css selector as below
driver.findelement(By.CssSelector("[data-val='BB9049_600']"))
also by looking at 'data-val' value it seems 600 in 'BB9049_600' is not a constant value if that is the case then you can use below to check if selector starts with value
driver.findelement(By.CssSelector("[data-val^='BB9049_']"))