How do I find elements hidden by knockout with Selenium - selenium

I have a "Logout" button in a user portal that I don't get to grab.
The frontend is knockout.js and the framework for testing is Selenium.
Now I can get many elements via the ID, but not this logout button.
A lot of help on the Internet is already outdated and I just can't get any further.
<button name="logout" class="btn btn-primary" data-action="userLogout" data-bind="click: logout" data-i18n="app:modules.localization.generals.button.logout">Abmelden</button>
This is the source code of the button.
For example, if I execute this code
webDriver.FindElement(By.Id("logout")).Click();
I get this message
Message: Test method MyFirstAutomaticTest.UnitTest1.StartPage threw exception:
System.InvalidOperationException: Cannot click on element (MoveTargetOutOfBounds)
Can someone help me with that?

As per the HTML you have shared to click() on the element with text as Abmelden you can use either of the following solutions:
cssSelector:
webDriver.FindElement(By.CssSelector("button.btn.btn-primary[data-action='userLogout']")).Click();
xpath:
webDriver.FindElement(By.XPath("//button[#class='btn btn-primary' and #data-action='userLogout']")).Click();
Note: As the element is an Angular element you have to induce WebDriverWait for the desired element to be clickable.

You have bind name attribute as ID,
Try this,
webDriver.FindElement(By.name("logout")).Click();

Try with xpath
webDriver.FindElement(By.Xpath("//button[text()='Abmelden']")).Click();

Related

checking checkbox with selenium ElementNotInteractableException: Message: element not interactable

I wanna log to this page automatically an website
but I cant check the checkbox with selenium with this code
driver.find_element_by_name('login[terms]').click()
and the checkbox html code is:
<input type="checkbox" name="login[terms]" value="1" id="cbx1">
and I recieve:
ElementNotInteractableException: Message: element not interactable
According to your code I recommend two options
Verify using find by id instead by class name and check if it works or not with the following code
driver.find_element_by_id('cbx1').click()
Your code is correct but is recommended to implement an explicit wait to verify and confirm that your element is interactable at the point that you want to use it so that you give a chance to the page to load the element correctly, you can achieve this with the following code
WebDriverWait wait = new WebDriverWait(driver,TimeOut);
WebElement chkBox = wait.until(ExpectedConditions.visibilityOfElementLocated(By.Id( "cbx1")));
chkBox.click();

Getting an error when clicking a link element on a web page by a Selenium script

When my Selenium script clicks a link element presented on a web page by click() method, I am getting the below error:
org.openqa.selenium.WebDriverException: Element is not clickable at point (36, 72).
This is my HTML code
<div id="targettab">
Book
</div>
This is my Selenium code:
driver.findElement(By.id("highlight-book")).click();
What am I doing wrong here? Could you please advise me about possible solutions? Thanks.
If element is present in lot of nested divs, rarely selenium driver will fails to click element. You can try clicking using Enter button.
driver.findElement(By.id("highlight-book")).sendKeys(Keys.Return);

Behat to click on element by class or ID name?

Is it possible for behat to find an element by class name to click on? It looks like only the following is searched for: id|name|title|alt|value
For example, how could you successfully identify this element to click on?
<a class="button medium round signup" href="http://link.com" data-reveal-id="signupModal">sometext</a>
Also here is a simple page with a button, that has an ID. How come the following does not access the button?
<button id="myBtn" type="button" onclick="myFunction()">Try it</button>
Given I am on "http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_button_form"
When I press "myBtn"
Thanks
The button can not be accessed because it is contained in an iframe, you need to switch to that iframe to make the button available.
You can create steps to click on an element by any type of identifier (class included), or you can implement new step that requires css/xpath selectors.
Here you can find an example of a method to click by selector.
The first HTML code is nothing but a HREF. Behat should be easily able to identify it by using the line below:
Given I follow "sometext"
If the above doesn't work, you can simply use the CSS selector and click the element:
$locator = '.';
$this->getSession()->getPage()->find('css',$locator)->click();

selenium click using span class or onclick

I am a newbie to java and selenium webdriver. I am having an issue clicking an image. Below is the page source.
<a href="javascript:void(0);">
<span class="HomeButton" onclick="javascript:onBtnHomeClick();"/>
</a>
I tried below codes but did not work and still getting the Unable to locate element error.
driver.findElement(By.xpath("//a[#onclick='onBtnHomeClick()']")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("HomeButton"))).click();
I have to click the homebutton. Any help would be much appreciated
I don't know why By.className("HomeButton") didn't work but you have errors in the other two.
In driver.findElement(By.xpath("//a[#onclick='onBtnHomeClick()']")).click(); the tag for onclick is <span> not <a>. It also not onBtnHomeClick() but javascript:onBtnHomeClick();
driver.findElement(By.xpath("//span[#onclick='javascript:onBtnHomeClick();']")).click();
If you want to use onBtnHomeClick() use contains
driver.findElement(By.xpath("//span[contains(#onclick, 'onBtnHomeClick')]")).click();
Or
driver.findElement(By.cssSelector("onclick*='onBtnHomeClick'")).click();
And in wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click(); the <span> parent tag is <a>, not <div>
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/a/span"))).click();
You simply need the correct locator IF your element will be eventually visible.
Xpath = "//span[contains(#class,'HomeButton') and contains(#onclick,'onBtnHomeClick')]"
Add wait as needed above exanmple, that should work.

selenium code for accessing button type submit

i want to access the submit button having code
<input class="btn btn-primary" type="submit" value="Save">
I have tried for ;
driver.findElement(By.name("Save")).click();
and
driver.findElement(By.name("Save")).submit();
but raising an error of : No such element Unable to locate Element
Your element has no name, it has a tag and some class names which may or may not be unique across the page you are testing. These may work but we might need to see more of the page.
driver.findElement(By.className("btn-primary")).click();
driver.findElement(By.cssSelector("btn.btn-primary")).click();
You may find the page http://docs.seleniumhq.org/docs/03_webdriver.jsp useful as it gives you an introduction to the different ways to find elements on the page.
Add a sleep time or wait before the submit.
Thread.sleep(5000);
driver.findElement(By.name("Save")).click();
I think this will solve your problem