How do I create a logic for href followed by text - selenium

Passed
How do I find the element based on the text followed by the href?
I have tried this
IWebElement ele = Driver.driver.FindElement(By.XPath("//a[contains(#href,'grdCoverageSelected' and text(),'Passed']"));
ele.Click();
Also, there are some other controls like
Passed,TBD,Pending,and failed
Also, need to implement a logic if I find passed then it will go to next page and if I will find any other of the tags then it will click the hyperlink and change from TBD to PAssed or failed to passed or pending to passed from the drop-down list

Please try with below solutions.
using xpath = //a[#href='href url'][contains(text(),'text')]
using css = div:contains("Click here") or div>.className:contains("Click here")

Related

Is there a way to click on anchor link

Please help me as the anchor tag looks like the below
<a title ="excel" class="activelink"style="Text-Decoration: none; onclick="$find('ReportViewerControl').exportReport('Excelopenxml');" href ="javascript:void(0)" alt="Excel" _selected="true"> Excel</a>
This doesn't have any document id or class.. Any help would be highly appreciated.
I try to check your HTML code and found that it contains the class but it can be possible that many other elements on the page using the same class. If you try to access the link using that class then it can possible that you click the incorrect element on the page.
We can see that the link contains the ' Excel' text. We can try to loop through all the links on the page and try to match the innerHTML to find that specific link.
Example:
'Below is code to loop through anchor tags, find the specific link, and click it.
Set elems = IE.document.getElementsByTagName("a")
For Each elem In elems
If (elem.innerHTML) = " Excel" Then
elem.Click
Exit For
End If
Next elem
Output:
In a similar way, you can also match other attributes from the anchor tag that may also help to click the link.
Note: This code example is for clicking the specific link on a page. It may not help you to automate the file download.
Further, you can try to modify the code example as per your own requirements.

selenium send key for xpath not working

I want make automation for this web site
in this web site 3 text box are here check image
1st text box x path is /html[1]/body[1]/div[3]/div[1]/div[2]/div[1]/searchbar[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[1]/input[1]
here is my code
driver.findElement(By.xpath("/html[1]/body[1]/div[3]/div[1]/div[2]/div[1]/searchbar[1]/div[1]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[1]/input[1]")).sendKeys("rio salon");
when I run this code I got this error
Exception in thread "main"
org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard
How can i fix it? I hope my xpath is correct.
The field has aria-hidden=true attribute, so you get ElementNotInteractableException. You need to click on the dropdown first to change the attribute to true
WebElement dropdown = driver.findElement(By.xpath("//*[#id='search-form']/div/div[1]/div/div[1]/span"));
dropdown.click();
WebElement textField = dropdown.findElement(By.xpath("./parent::div/following-sibling::input[contains(#class, 'ui-select-search')]"));
textField.sendKeys("rio salon");
You can click in an input field with a div or span tag, but you cannot type in the field. So, your XPath must be written with an input tag if you want to sendkeys or type in an input field. For example:
//input[contains(#placeholder,'Site')]

Get text associated with check box in selenium where html code does not have value or name property

I have a Radcombobox in my application which has several check boxes in the combo box. What I wish to do is to fetch the text associated with the check box. I searched on internet and came to know that text can be fetched from name or value property in HTML code, but problem is that my HTML code does not have such properties.
HTML code:
input class="rcbCheckAllItemsCheckBox" type="checkbox"
Check All
What i wish to do is to fetch value "Check All".
Using code x = driver.findElement(By.xpath("/html/body/form/div[1]/div/div/div/input")).getText();, value returned is blank.
You can get the text through JavaScript API of Rad Controls. You can check the official documentation- http://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/client-side-programming/overview
Basically, you first locate the element in JS and then use the official control method, in your case get value method. You can also perform several other useful operations if you need to.
You can execute JS in WebDriver with the following code:
IWebDriver driver;
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string title = (string)js.ExecuteScript("return document.title");

Having issue in selecting drop down element under makemytrip website

Having issue in selecting drop down element under the following website
http://flights.makemytrip.com/makemytrip/fareCal.do?intid=NewHP_to_DF_FC_Menu
I'm unable to select any one of the cities listed below.
Please help me out resolving the same.
Scenarios Tried
driver.findElement(By.className("chzn-single")).click();
driver.findElement(By.xpath("//span[contains,'NewDelhi']")).click();
driver.findElement(By.xpath("//span[#id='fromcity_chzn']")).click();
This works:
WebElement leavingFrom = driver.findElement(By.xpath("//*[#id='fromcity_chzn']/a"));
WebElement goingTo = driver.findElement(By.xpath("//*[#id='tocity_chzn']/a"));
leavingFrom.click();
leavingFrom.sendKeys("Bangalore");
leavingFrom.sendKeys(Keys.RETURN);
goingTo.click();
goingTo.sendKeys("Goa");
goingTo.sendKeys(Keys.RETURN);
Here is working sample:
//First get main dropDowns
var leavingFromDropDown = driver.FindElement(By.Css("#fromcity_chzn"));
var goingToDropDown = driver.FindElement(By.Css("#tocity_chzn"));
//Select value from first dropDown using dropDown items index
//First click on dropDown to open it
leavingFromDropDown.click();
//Now find items in it and click on any item using it's index (also can be used method to access this elements using their names
leavingFromDropDown.FindElements(By.Css(".active-result"))[1].click();
//this dropDown closes automatically, but if not you need to click on it again to close
//Same perform with second element
goingToDropDown.click();
goingToDropDown.FindElements(By.Css(".active-result"))[2].click();
If you want to use input box to enter any value from DropDown you need to find that element and using sendKeys set it's value. E.g.:
leavingFromDropDown.click();
var input = leavingFromDropDown.FindElement(By.Css(".chzn-search > input"));
input.sendKeys('Goa');
input.sendKeys(Keys.Enter);//or tab

How to manipulate user selected text using webdriver?

Lets say i have the following snippet in my web page:
<p> This is some text </p>
I want WebDriver to select "some" in this text, as if the user selected it. How should i do this? I know how to get the <p>-element:
WebElement editable = getDriver().findElement(By.id("someId"));
editable = editable.findElement(By.tagName("p"));
System.out.println(p.getText());
The println prints "This is some text".
I tried sending keys to the element, and that used to work(in selenium 2.0b), but i'm using selenium 2.6.0 now, and it stopped working:
editable.sendKeys(Keys.chord(Keys.SHIFT, Keys.LEFT));
Does anyone have ideas? I'm using the FirefoxDriver.
I did this once for Firefox using Javascript. Basically I used the range object in Firefox to select the text. Change the start and end range index based on what you want to select. This would not work in IE, because selecting range is conceptually different in IE. I don't have the IE code handy but since you are concerned about FF, you could give this a shot.
Let me know if you interested in IE text range.
String script = "var range = document.createRange();" +
"var start = document.getElementById('idofthedivthatcontainstext');" +
"var textNode = start.getElementsByTagName('p')[0].firstChild;" +
"range.setStart(textNode, 8);" +
"range.setEnd(textNode, 13);" +
"window.getSelection().addRange(range);";
((JavascriptExecutor)driver).executeScript(script);
You are trying to select the contents of the p tag by drag select right I am not sure if that is objectively possible to be done by the user as your are suggesting.. Selenium now tries to mock the exact action that a user can perform on a browser. thus you just cant send a shift and left select key on the p tag and expect it to select unlike a textbox where it is very much possible but you might probably have to click on the text box first to get it into focus.
Here is what I would suggest to achieve this, not sure if it will work.
a) send the left click on the p tag
b) hold the shift key
c) drag the mouse to the end of the p tag.
Hope that helps.
Use the .Text property of the IWebElement
WebElement editable = getDriver().findElement(By.id("someId"));
editable = editable.findElement(By.tagName("p").Text).ToString();
editable.Replace("This is ", "").Replace(" text.");
System.out.println(p.getText());