Protractor When sendKeys meet issue 'cannot focus element' - selenium

I am not sure if Protractor can sendKeys to a div.
My code is
element(by.css('.create-api-schema-editor .CodeMirror .CodeMirror-lines .CodeMirror-code .CodeMirror-line')).sendKeys(content);
I also tried click first and then sendKeys, but no luck.
HTML is:
enter image description here

I fixed the issue, we can use browser.actions().click().perform() instead of ele.click().
I am not sure why but it works.
Hope this helpful for you.

Related

How can I access a button to click it with following code in selenium webdriver?

There are many buttons sharing the same class.
May have to go with clicking the link /account/logout'.
This is the code I'm trying to use:
input class="btnRed text-uppercase fo_white" value="Logout" onclick="window.location.href='/account/logout';" type="button"
Hard to say because you didn't provide enough info, but you could probably make it work by using value attribute. Something like this if you are using java.
driver.findElement(By.cssSelector("[value='Logout']")).click();
Not pretty solution, but give it a try:
driver.findElement(By.cssSelector(".btnRed.text-uppercase.fo_white")).click();
Looking at your HTML DOM this command will work for you:
driver.findElement(By.xpath("//input[#value='Logout']")).click();
Let me know if it works for you.
Looking at your HTML, this should work
driver.findElement(By.xpath("//input[#value='Logout'][#type='button']")).click();
Let me know if it works.
Is your element visible/enabled? In order to select an element, it must be present in your DOM. If the element is activated through an event, it cannot be selected until the event occurs.
Take a look at this post. This other post also have good ideas.

Selenium Webdriver Onlink Text

What command in webdriver should I use to make sure it clicks the specific text i.e Last Month
the code is
<div class="dt_padded_medium_div">
<a onclick="setLastMonth()" href="#">Last Month</a>
I tried xpath by using firepath but still doesnt work
it was
//*[#id='block-2']/div/div[3]/table/tbody/tr[1]/td[5]
I used
driver.findElement(By.xpath("//*[#id='block-2']/div/div[3]/table/tbody/tr[1]/td[5]")).click();
but still didnt work, am I missing something?
Update:
Got the Code working guys, thanks for the help!
If there is only one 'a' element in the page with this text, try this XPath:
//a[text()='Last Month']
If there are more than one element, please, post the full HTML tree else we're unable to write a xpath without know the tag path and ensure it will work
Are you sure your xPath is correct?
Install FirePath on Firefox and experiment with your xPath as on screenshot below.
Also, that does look like a long and brittle xPath. Look up xPath cheat sheets to learn how to make your xPath more robust.
In your case I would imagine something like:
//*[#id='block-2']/descendant::a[text()='Last Month']
(get the element with a particular ID, then search for an <a></a> in that element, no matter how deep, with a particular text)
In your question, you have used a "id" that is not shown in your code, where is #id='block-2'. It is possible for you to have used the wrong id. For us to help you, can you please provide the whole HTML code?
What I can suggest based on the information you have provided is:
Making sure the Xpath you have provided is unique, there is an add-on for Firefox called Firebug you can use. It will help you find out the xpath you are after fast and easy. What you need to do is basically:
download firefox and install it;
download firebug add-on and install it to firefox;
you will notice there is a small bug symbol to the top right of your tool bar, please enable it;
you will see a console pops out, click on inspection button and click on any web element you want to inspect and its html code will be highlighted in the console;
right click on the highlighted code and choose to copy its xpath to clipboard. This way you will never get your xpath wrong.
Here is a quick tutorial:
http://www.wikihow.com/Find-XPath-Using-Firebug
P.S. There are more than one way to locate a webelement, please consider using the following options as well:
Css selector
class name
id
This link will direct you to an awesome cheat cheet.
http://scraping.pro/res/xpath-cheat/xpath_css_dom_recipes.pdf
Hope it helps.
Please try to use below code to click on the text Last Month
driver.findElement(By.xpath(".//a[text()='Last Month']")).click();
Hope this helps.
You can use below code to click on the link containing the text 'Last Month'
driver.findElement(By.linkText("Last Month")).click();
First of all make sure that your xpath works correctly, try to use more specific xpath other than just td[5] or smthn like that, for example above could be like:
//div[#class='dt_padded_medium_div']/a[#href] - meaning, we need to find <div> element with specific parent and within it <a> element that has href property
There are a few possibilities.
If you're site doesn´t get rewritten to much you could just go for the link name via
driver.findElement(By.linkName("Last Month");
You could also go for any of the attributes if your site is subject of many rewrites like this and just put "href" and "#" or "onclick" and "setLastMonth()" as arguments when calling it.
static WebElement getLink(String Attribute, String Value /*String ItemText*/){
List<WebElement> Elements = driver.findElements(By.tagName("a"));
for(int Counter = 0, Counter < Elements.size(); Counter++){
if(Elements.get(Counter).getAttribute(Attribute).contains(Value) /* && Elements.get(Counter).getText().equals(ItemText){
return Elements.get(Counter);
}
}
return null;
}
If you remove the comments it will go completely sure and check for the Attributes and the displayed text.

Click Button With No ID Selenium Java

Apologies for this;
Event Triggers
Photos
Push Notifications
How can I Click on Event triggers for example using Selenium Java?
I've tried this code:
By.xpath("//button[contains(text(),'Event triggers')]");
By.xpath("//button[contains(text(),'Submit')]");
I tried this as well:
WebElement Box= driver.findElement(By.tagName("Event triggers"));
Box.submit();
Neither of these worked...
Thank you
Try to using this:
By.xpath("//a[contains(text(),'Event triggers')]");
instead of
By.xpath("//button[contains(text(),'Event triggers')]");
Based on the page source mentioned in the comment I think Event Triggers is not a button but an tag which essentially makes it a link.
As you know the text of the link you are trying to click you can always use:
driver.findElement(By.linkText("Event Triggers"));
to get Event Triggers web element.
Using xpath should be avoided due to slow performance.
Well, if button doesnt have any id , you can try to write dynamic xpath by using "class name" and "type" (there are plenty of examples , you can learn easily how to create your xpath) , or easiest way, use firebug to locate element that you want to click and copy exact Xpath via firebug. And then click.
The correct answer is:
By.xpath("//button[contains(text(),'Event triggers')]").click();
You were missing that click action.

click on submit button not working in selenium webdriver

I trying to click on the create account button in registration form.
this is how the button locate in the html page:
<div id="submitContainer"><button type="submit" class="large"><span><strong> Create Account </strong></span></button></div>
this is the button xpath:
//*[#id="submitContainer"]/button/span/strong
the problem is that the button don't have id, he locate inside a div.
I try to use by id,xpath,css,name, but all of this not working:
driver.findElement(By.id("submitContainer")).click();
driver.findElement(By.xpath("//*[#id='submitContainer']/button/span/strong")).click();
driver.findElement(By.tagName("Create Account")).click();
driver.findElement(By.className("large")).click();
thanks!
In your examples, except for the last one, you are not targeting the button. Now your last example, should actually locate the button-element:
driver.findElement(By.className("large")).click();
Could you please post the error message you are getting?
Are there more than one element on the page with className "large"?
Make sure the button is in view window, if it is then try clicking on it. Try to wait for the element to load. There might be an issue with your element being loaded into DOM -
driver.wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[#type='submit']"))).click();
Hope this helps.
If you want to use xpath, the correct syntax is
//button[#type='submit']
Use this line below:
Thread.sleep(3000);
I got the result once I used this one. Since some time we need to give some sleep time for the site to load fully to pull the Xpath.
You can use the linkText
driver.findElement(By.linkText("Create Account")).click();
Hope it will work for you.

mouseOver do not works in IE for selenium RC

I am trying to do mouse over an element in IE9 browser. After mouse
hover a toll tip will open. To do this I am using:
selenium.mouseOver("css=a.worldwide");
It works fine, in FF browser. But fails in IE browser. I am using:
Selenium RC 2.18.0. I have also try this by both Xpath and CSS
locator. But it fails.
Can any body please help me how can I resolve this issue?
This may be not answer to your question but may help you.
getAttribute("css=a.worldwide#title")); // getAttribute(locator#attribute name)
This function will return the text available for title attribute for the element specified.
Regards
Using javascript/jQuery I managed to work around the problem:
selenium.eval('selenium.browserbot.getCurrentWindow().jQuery("a.worldwide").trigger("mouseover");')
I'm using python, so you may need to adjust quotes.
I have no idea what jQuery does that selenium doesn't, but this made the problem go away for me on Saucelabs & Selenium 1.