Approve
How can I get this element in selenium C#. I tried by:href ,Xpath, ID, class name, text name but I couldn't get this element
you can click by Link text
driver.FindElement(By.LinkText("Approve")).Click();
or by CSS Selector
.btn.green.request-action-btn
To identify the element you can use the following Locator Strategy:
CssSelector:
driver.FindElement(By.CssSelector("a.btn.green.request-action-btn[data-request-status='Approved']"))
XPath:
driver.FindElement(By.XPath("//a[#class='btn green request-action-btn' and #data-request-status='Approved']"))
XPath using contains():
driver.FindElement(By.XPath("//a[#class='btn green request-action-btn'][contains(.,'Approve')]"))
XPath using normalize-space():
driver.FindElement(By.XPath("//a[#class='btn green request-action-btn'][normalize-space()='Approve']"))
It can be found by using tagName as below:
ReadOnlyCollection<IWebElement> anchrTags= driver.FindElements(By.TagName("a"));
IWebElement roWebElement = anchrTags.Where(x => x.Text == "Approve").FirstOrDefault();
Otherwise if the anchor tag exists in the frames, it will not found. try switching to that frame:
IWebElement iframe = driver.FindElements(By.Id("iframeId"));
driver.SwitchTo().Frame(iframe);
IWebElement roWebElement = anchrTags.Where(x => x.Text == "Approve").FirstOrDefault();
Related
WebElement SearchField =
driver.findElement(By.xpath("/html/body/div[2]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input"));
SearchField.sendKeys("my name");
Thread.sleep(5000);
WebElement SearchButton =
driver.findElement(By.xpath("/html/body/div[2]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]"));
SearchButton.submit();
Thread.sleep(3000);
driver.quit();
In above code I have tried to select the search box in google.com and enter "my name" as the value.
but it says xpath element not found.
So I took the "name" instead of xpath element and it worked.
WebElement SearchField = driver.findElement(By.name("q"));
How can I run this using xpath?
Why you want to switch to xpath if name works fine ?
There are lot of advantage if you use ID, NAME, LINKTEXT, PARTIALLINKTEXT, CSSSELECTOR, CLASSNAME and lastly XPATH :
Anyway you can try this xpath :
//input[#name='q']
this is a relative xpath. The one you are using is absolute xpath. try to avoid that.
The unique XPath for the search input element you are looking is:
//input[#name='q' and #type='text']
I want to get text/value from the tooltip which appears when hover on svg element on the graph created with highcharts.
Below is the snippet:
Tried below code:
List<WebElement> Volumelist=driver.findElements(By.xpath("//*[name()='svg']//*[name()='g'][5]//*[name()='g'][2]//*[name()='path']"));
System.out.println("Got the list!");
new Actions(driver).moveToElement(Volumelist.get(1)).clickAndHold().build().perform();
WebElement toolTip=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class='highcharts-halo highcharts-color-3'][#visibility='visible']")));
System.out.println("toolTip text= "+toolTip.getText());
First hover on the element on which tooltip appears and then execute :
String tooltipText = driver.findElement(By.cssSelector("g.highcharts-tooltip text tspan")).getAttribute("textContent");
You can ping me at sandpdangi13#gmail.com if that does not work for you.
Try to hover to tooltip element using this method if normal Actions hover is not working :
public static void mouseHoverJScript(WebDriver driver, WebElement element) {
String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false);"
+ " arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
((JavascriptExecutor) driver).executeScript(mouseOverScript,
element);
I found over their examples page the values with this XPath:
driver.findElement(By.xpath(.//*[name()="g" and contains(#class,"highcharts-label")]//*[name()="tspan" and #style='font-weight:bold'])).getText();
The first step is move the mouse over one point, for example with this XPath, move to the first point:
WebElement elem = DriverUtils.driver.findElement(By.xpath(.//*[name()='path' and contains(#class, 'highcharts-point highcharts-color')][1]));
new Actions(driver).moveToElement(elem).clickAndHold().build().perform();
I used the Selenium 3.9.0, with previous 3.4.0 version doesn't work for me.
So, solution that worked for me:
WebElement elem = driver.findElement(By.xpath("//[name()='svg']//[name()='g'][5]//[name()='g'][2]//[name()='path'][1]"));
new Actions(driver).moveToElement(elem).clickAndHold().moveByOffset(1, 1).pause(1000).perform();
String text=driver.findElement(By.xpath("//[name()='svg']//[name()='g'][9]//[name()='text'][1]//[name()='tspan'][3]")).getText();
Using firefox and marking a link in my web-app I get among other things, this code which I think I can use to caprture an object:
cb_or_somename_someothername cb_area_0219
This string is "classname" in Firebug.
Going to the script I type in:
WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername cb_area_0219"));
But the script does not find the element when executing.
Other onfo in the Firebug panel is:
class="cb_or_somename_someothername cb_area_0219"
onclick="jsf.util.chain(this,event,'$(this).attr(\'disabled\', \'disabled\');return true;','mojarra.jsfcljs(document.getElementById(\'fwMainContentForm\'),{\'fwMainContentForm:j_idt156:2:selectRole \':\'fwMainContentForm:j_idt156:2:selectRole\'},\'\')');return false"
href="#"
id="fwMainContentForm:j_idt156:2:selectRole"
Is my script referring the element in a wrong way?
You cannot use search by compound class name (name with spaces). Try to use search by CSS selector instead:
WebElement rolle = driver.findElement(By.cssSelector(".cb_or_somename_someothername.cb_area_0219"));
or by XPath:
WebElement rolle = driver.findElement(By.xpath("//*[#class='cb_or_somename_someothername cb_area_0219']"));
Also you still can use search by one of two class names:
WebElement rolle = driver.findElement(By.className("cb_or_somename_someothername"));
or
WebElement rolle = driver.findElement(By.className("cb_area_0219")); // Note that this class name could be generated dynamically, so each time it could has different decimal part
Update
If you get Element is not clickable... exception it seem that your element is covered by another element at the moment you try to click on it. So try to wait until this "cover" is no more visible:
new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//p[#class='environtment-banner']"));
WebElement rolle = driver.findElement(By.className("cb_area_0219"));
rolle.click();
I have this element:
WebElement element = ...
string val = element.getAttribute("innerHTML");
All I want to do is to change this innerHTML on my web page.
Is it possible?
Try this:
WebElement element = ...
((JavascriptExecutor)driver).executeScript(
"var ele=arguments[0]; ele.innerHTML = 'my new content';", element);
Selenium WebDriver does not have any direct methods to do so to change the DOM itself. However we can use JavascriptExecutor to use javascript to modify the DOM.
check this example to change the background color. You will get an idea to change the innerHTML as well.
in python use this :
element = driver.find_element_by_id("some_id")
driver.execute_script("arguments[0].innerText = 'what_you_want_to_show'", element)
I want to get the value of div using webdriver and not Selenium
For example :
<div class="headerbande">BIENVENUE</div>
Is there any method in webdriver to get "BIENVENUE" using class name ?
Thanks in advance.
With java you would write:
WebElement element = webdriver.findElement(By.className("headerbande"));
Take a look at Introducing the Selenium-WebDriver API by Example for examples in other languages.
Thanks Volkerk, I found the solution via your post
WebElement webElement = driver.findElement(By.cssSelector("headerband"));
webElement.getText();
in ruby, you can locate the element using
css selector
web_element = driver.find_element(css: 'div.headerbande')
class
web_element = driver.find_element(class: 'headerbande')
id
# if your element's id is 'headerbande'
web_element = driver.find_element(id: 'headerbande')
It is also possible to get value/text by using xpath as below:
WebElement webElement = driver.findElement(By.xpath("//div[#class='headerbande']"));
webElement.getText();
OR,
You can get text/value by using css Selector as below:
WebElement webElement = driver.findElement(By.cssSelector("div.headerbande"));
webElement.getText();
you can use:
driver.findElementByClassName("headerbande").getText();