Selenium Web automation - Send values to Disabled text box - java

We are testing a ASPNET MVC Application by using Selenium.
In the Webpage they are using a Jquery Date control Textbox datepicker/#icon-trigger
and that text box was in Disable mode
I am trying to send the values from selenium to webpage by using following code.
Driver.findElement(By.id("txtDOB")).sendKeys("10/10/1986");
But it was not working. it doesn't show any error.
and Now My question is how to send values to Disabled text box?
Can any one help me on this...?

Well you have two approaches here
Do it like a user would do and use the query date picker see here (untested)
Enable the text box and then set the field something like this:
WebElement textbox = driver.findElement(By.id("xxx"));
((JavascriptExecutor) driver).executeScript("arguments[0].enabled = true", textbox);

I would really recommend that you script this so that it uses the date picker, in the exact same way a user would have to pick a date, as the user is not allowed to type in their own date.
If really must work around this, then you could try forcing a value into the textbox using javascript:
IWebElement textbox = driver.findElement(By.id("txtDOB"));
((JavascriptExecutor)driver).executeScript("arguments[0].value='10/10/1986'", textbox);

Related

Selenium Select By Visible text fails

my setup is the following:
I use Java8 with selenium webdriver for IE11.
I have an application of which I can't change the source, but I can see it.
when using xpath to select a specific dropdown in a table in an iframe in a popup, I can read it's attributes and tagname, but i can't read it's text using getText().
What's more, when I use
Select select = new Select(dropDown);
select.selectByVisibleText(value);
it does not work, nor the variant with
Select select = new Select(dropDown);
select.selectByIndex(valueOf);
However, when I do a simple reproduction, on a really basic html page with a select, I can read the text in the options.
This html page is loosely based on the popup page, where this specific dropdown is nothing fancy either.
Anyone has/had the same issue before?
all help appreciated

How to get the text from the auto suggestion of the auto complete text box?

My requirement is I want to select the particular name from the auto-suggestion in the autocomplete text box.
So here I only know how to get the name by using the mouse down to achieve this But I know it's not a good solution to get that because we don't give the guarantee to the auto-suggestion is same as all the time in the browser.
So if anyone knows how to get the auto-suggested text names for the auto-complete text box in Selenium Web Driver using Junit (Here I am using the Junit in Selenium WebDriver to develop the automation test script).
My code:
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ARROW_DOWN);
Thread.sleep(1000);
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ARROW_DOWN);
Thread.sleep(1000);
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ENTER);
Here the above code is only working for my correct option is shows as
the second option of the auto-suggested texts.
So that's why I need how to get the text names in the auto-suggestion for the autocomplete text box.
Please the give the solutions as the JUnit Form to my question because I am using the JUnit to develop the automation test script.
Thanks
The auto-suggest box is HTML like anything else on the page. Create a locator that finds the auto-suggest panel and then parse the contents. Once you figure out the structure, you can get the text that you need and then assert that it is correct.
I can't see the full HTML from your screenshot but you can see that the list is contained in an HTML UL. The UL is the container and each LI is a list item in the dropdown. You can use that info to do something like
ul.autoCompleteGroup > li
to get all the list items. I can't see what's inside of there but at some point you should be able to use .getText() to get the auto suggest items. From there you just compare what you pulled off the list to what you are expecting with an Assert.
Please try below code
public void selectAutoCompleteItem(String itemText) {
String xpathPattern = "//div[#id='phSearchInput_autoCompleteBoxId']" +
"//ul/li/a[.='%s')]";
String xpathExp = String.format(xpathPattern, itemText);
driver.findElement(By.xpath(xpathExp)).click();
}
In that case you can use the findelements functionality. So you can say:
List<WebElement> elements = driver.findElements(by.xpath("//a[#class='autoCompleteRowLin‌​k']");
And then for
each list item you can use getText to get the exact text. Then you can assert it with the expected values

Selenium Htmlunit org.openqa.selenium.ElementNotVisibleException: You may only interact with visible elements

enter image description here
Need support on issue selecting radio button, tried with javascript
but not working.
WebElement Select4 = driver.findElement(By.name("IsGoldMember"));
Select4.click();
Try driver.findElement(By.xpath("//input[#id='IsGoldMemberTrue']"));. It maybe the case that the name IsGoldMember is not unique on the web page.

Get ID, Class of Currently Focused Element in Selenium

There are many ways to select or focus an element in Selenium, for example using TAB key we can focus on next element.
But, is there any way in Selenium to get all details of current focused element such as id, class, href, text etc ?
i want to focus on Like, Comment or Share button of a post https://www.facebook.com/pitbull/photos/a.440436327400.230702.95051637400/10153236215477401/?type=3&theater of Facebook page of Pitbull, But nothing works for me, i tried xpath, class, id but unable to Focus on share button. i can focus on share button using Tab key about 161 times but how will i confirm that focused element is "Share" button or somethong else? ;)
Here is my sample code
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.id("email")).click();
driver.findElement(By.id("email")).sendKeys("myemail#yahoo.com");
driver.switchTo().activeElement().sendKeys(Keys.TAB);
after TAB key, you know focus will be go to Password field from email field, so how can i get id, class or other details of focused element in my selenium code? in my example it is Password field.
You can use
WebElement activeElement = driver.switchTo().activeElement();
String className = activeElement.getAttribute("class");
String id = activeElement.getAttribute("id");

How can i select text from browser screen with help of mouse events in selenium?

I am trying to select text which is already present on the browser.
I want to select that particular text and perform right click operation on it.
However, the page on the browser has disabled right click.
How can I select text in such situation?
Using a normal web browser without Selenium the only workaround that I can think about is to disable javascript to stop the script that prevents you from right clicking. So it should also work with a browser controlled by Selenium Webdriver.
I don't know if it will be OK for you because your website may be relying on javascript for essential features.
However if you don't need Javascript for your Selenium test you can try the following when you launch your driver :
FirefoxProfile p = new FirefoxProfile();
p.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(p);
I assume that you already know how to perform a right click because your question was only about dealing with the problem preventing you from doing this right click. But if not, you can also refer to this answer :
Select an Option from the Right-Click Menu in Selenium Webdriver - Java
Edit:
I'm sorry I really thought you could use Selenium actions to select the text you want but after some tests I didn't manage to perform a click and drag to select a text. The only thing that works for me in Chrome or Firefox is the following. It looks for a <p>which contains some text and then perform a double click to select a word.
driver.get("http://en.wikipedia.org/wiki/Java_(programming_language)");
WebElement text = driver.findElement(By.xpath("//p[contains(text(),'Java is')]"));
Actions select = new Actions(driver);
select.doubleClick(text).build().perform();
However it only highlighs one word in the html element that contains your text, so it's not really convenient.
I've also tried to do Ctrl+F and type the text so that the web browser automatically select it but the browser doesn't do anything when executing :
Actions search = new Actions(driver);
search .sendKeys(Keys.chord(Keys.CONTROL,"+f")).sendKeys("Java is").build().perform();
It seems that Selenium can only send keys events to the html elements and not to the browser (in the case of ctrl+F).
I don't really see a solution for now, let's see if someone else can find a workaround. It's an interesting issue, it would also be useful for me to select a text the way you described
Move to middle of the element
Actions builder = new Actions(webDriverObject);
builder.moveToElement(element).build().perform();
Move to starting of element, click and hold, move to end
Integer width = element.getSize().getWidth();
Actions newBuilder = new Actions(webDriverObject);
newBuilder.moveByOffset(width/2,0).clickAndHold.moveByOffset(width,0).release().build().perform();

Categories

Resources