find specific element for button using selenium for web scraping - selenium

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')]"));

Related

How to click on send button on whatsapp using Selenium xpath or css in vb.net

How can use whatsapp xpath or css(Chromedriver) in vb.net.
HTML:
<span data-testid="send" data-icon="send" class>...</span> ==$0
xpath:
//*[#id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[2]/button/span
I tried but not working:
driver.FindElement(By.CssSelector("span[data-testid='send' data-icon='send']")).Click()
As per the HTML:
<span data-testid="send" data-icon="send" class>...</span>
To click on the element you can use either of the following Locator Strategies:
Using FindElementByCss:
Driver.FindElementByCss("span[data-testid='send'][data-icon='send']").Click()
Using FindElementByXPath:
Driver.FindElementByXPath("//span[#data-testid='send' and #data-icon='send']").Click()
References
You can find a couple of relevant detailed discussions in:
How to click on the element using Selenium and VB.Net
it should be like
driver.FindElement(By.CssSelector("span[data-testid='send'][data-icon='send']")).Click()
when select specific attributes :
span[data-testid='send']
to add attributes to same tag :
span[data-testid='send'][data-icon='send']
*no space between attributes

Finding Xpath Selenium

I have the following element:
<div class="PickList visible"
widgetid="Palette" id="Palette">
<span class="stuff">
<span class="stuff"><span class="lbl">A-B</span><span class="no">1111</span>
</span>
<span class="stuffSelect"><span class="lblSelect">C</span><span
class="plu">2222</span></span>
The xpath that I am using is:
Driver.driver.findElement(By.xpath("//*[#id="Palette"]//span//span[2]//span[contains(text(),'C')]"));
It's still not able to pickup the letter 'C'.
Any suggestions appreciated. Thanks.
you can try below xpath to track from div.
driver.findElement("//*[#id='Palette']/span[2][#class='stuffSelect']/span[1][contains(text(), 'C')]");
xpath you are using is incorrect. I am providing you with the correct xpath or you can directly fetch it using the className as well.
Updated xpath as per the discussion:
WebElement selectedCharacter = driver.findElement(By.xpath("//div[#id='Palette']//span[#class='lblSelect']"));
selectedCharacter.getText();
By using className:
WebElement selectedCharacter = driver.findElement(By.className("lblSelect"));
selectedCharacter.getText();
Please try the below Xpath.It will print you 'C'
driver.findElement(By.xpath("(//div[#id='Palette']//span[1]//span[2]/span)[1]")).getText()
Here is the xpath. Consider the class visible in your xpath or css. As this denotes that this div might be not visible sometimes. So always be sure to use the visible if you have it as part of the class.
CSS
div.PickList.visible span.lblSelect
xpath
//div[#class='PickList visible']//span[#class='lblSelect']

how to locate the nested span element locator

how to locate only parent span which is containing the price value from the below Code,locator strategy doesn't matter:
<div class="price-box price-margin">
<p class="old-price"></p>
<p class="special-price">
<span id="product-price-75254" class="price">
56,90 €
<span class="steuerstern">*</span>
</span>
</p>
</div>
The issue is not the selector here. But the text function.
//p[#class="special-price"]/span[#class="price"]
This will select the node, but selenium will always return the node of the child nodes also. What you are looking for is the node text in this case. This can only be done using JavaScript. Since you didn't mention a language I am going to assume python
elem = driver.find_element_by_xpath('//p[#class="special-price"]/span[#class="price"]')
price = driver.execute_script("return arguments[0].firstChild.textContent.trim();", elem)
print(price)
56,90 €
Using selector you can go like this:
".price-box span.price"
Result:
56,90 €
To test the xpath correctness in Chrome you can use
//span[#class="price"]/text()
Since you didn't mention the language you use , here is an example in nightwatchjs with selenium by using .getText()
.getText() is returning an object, the actual text is the value of callback.
var thetext='';
.getText('//span[#class="price"]' ,fucntion(result){
thetext=result.value();
})
As you haven't mentioned the Selenium bindings you are using so I will construct an Answer through Java bindings. Here we will use a customized xpath to locate the node with text 56,90 € with out the * as follows:
WebElement ele = driver.findElement(By.xpath("//p[#class='special-price']/span[contains(#class,'price') and not(#class='steuerstern')]"));

How to create an XPath for this, suitable for Selenium?

Please help me to create the Xpath for the following. I am using Selenium webdriver and have only IE browser. Following is the HTML:
<A
title=""
href="javascript:Plg({u:'/epace/starintfc.html?module=RUN_EVENT',p:0,nw:'0'});"
p="eagle/pace/pace"
la=""
ml="%09NULL%09PLUGIN%09%2fepace%2fstarintfc.html%3fmodule%3dRUN_EVENT%09%09%09%09%09%09%09%09%09%09"
t="Plugin" gh="" g="Data Steward"
pc="Eagle/PACE/PACE Components"
ivname=""
><IMG id=Item.Img
class=link
src="/tpe/modules/site/img/menu_plugin.gif">Run An Event</A>
As I prefer CSS over xPath because of it's simplicity, I will provide both the CSS and xPath solution:
Remember that you want to get the attribute of the elements that have the most specificity. The answers below are the best of my ability, to determine which have the most specificity. (see here if you want to learn more about CSS and specificity of elements)
If you are trying to select the first <a> element...
CSS:
a[href*='RUN_EVENT']
xPath:
//a[contains(#href, 'RUN_EVENT')]
If you are trying to select the <img> that is inside of the <a> tag...
CSS:
a[href*='RUN_EVENT'] > img
xPath:
//a[contains(#href, 'RUN_EVENT')]/img
My guess is that you want the first one since it's a link, and I assume you want to invoke a click on it.

Linktext in selenium webdriver

Is it possible to use linkText locator in this code
I used driver.findElement(By.linkText("welcome")).click();
But it didn't work.
Please help....
<div class="back-to">
<a class="button blue" href="javascript:history.back()">welcome</a>
</div>
The linkText should work in this case. Or else try the below alternatives(and please provide sufficient implicit timeout to give selenium sufficent time to detect the element):
1. Using xpath, to click on the element 'a' with exact innerHTML/text as 'welcome':
driver.findElement(By.xpath("//a[.='welcome']")).click();
2- Using JavascriptExecutor to click on the element with exact innerHTML/text as 'welcome':
((JavascriptExecutor)driver).executeScript("arguments[0].click();", driver.findElement(By.xpath("//a[.='welcome']")));
3- Using partialLinkText to click on the link with partial text 'welcome'
driver.findElement(By.partialLinkText("welcome")).click();
This Should be enough:
driver.find_element_by_xpath('//a[#class="button blue"]').click();