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

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);

Related

Unable to click "Accept all" cookies in Selenium(python) in a pop-up

I am trying to click the Accept button on the pop-up for cookies.
Here's the code that I have tried:
driver.get(r'https://www.studydrive.net/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.sc-gtsrHT.iETHdM"))).click()
Here's the error:
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
I have also tried using X-path but was not able to click on the button.
Any help is highly appreeciated.
This pop-up is on the shadow dom.
Selenium does not provide explicit support to work with Shadow DOM elements, as they are not in the current dom. That's the reason why we will get NoSuchElementException exception when try to access the elements in the `shadow dom.
With the following JavaScript this should work:
driver.get(r'https://www.studydrive.net/')
time.sleep(5)
accept_all_btn = driver.execute_script('''return document.querySelector('#usercentrics-root').shadowRoot.querySelector('button[aria-label="Accept All"]')''')
accept_all_btn.click()
See more explanations here and here

Selenium cssselector shows correct webelement but script always run exception no such element

I got stuck not just for this one case. Many cases came out with nosuchelement exception, I cannot find the reason why. I use chrome console to locate the element and it shows correct result. Is that possible something wrong with the page itself, not my script?
Context click on the element you are trying to click, and see if the element is inside any frame. Try to navigate through the tags and see if this element is under any iframe.
switch to the iframe using,
driver.switchTo().frame(driver.findElement(By.xpath("iframexpath")));

How do I find elements hidden by knockout with 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();

Can't click to Customize link when div+link together with FakeAnchor class using selenium web driver

Can't click to Customize link when div+link together with FakeAnchor class using selenium web driver
In my Ajax application, we have dropdown + link (Customize) together in div and i want to click to Customize link. I have locator and which was working fine for Customize link with old selenium but it doesn't with latest web driver. Can anyone please point me the problem or suggest something to make it work?
Expected:
Clicking to Customize link should open respected option (it actually opens dialog).
Actual:
Below locator clicks to dropdown button instead of Customize link due to such a complex page DOM which has no actual href or anchor tag.
Locator:
css=div[id$='_repeatDesc'][class='FakeAnchor']
Html:
<div id="zcs1_repeatDesc" class="FakeAnchor" style="cursor: pointer;">Customize</div>
Code:
webDriver().findElement(By.cssSelector("div[id$='_repeatDesc'][class='FakeAnchor']")).click();
I think your locator is not unique, may be it is locating dropdown element that's why it clicks to dropdown button instead of Customize link.
You should try using By.xpath() with text() node to locate this element as below :-
webDriver().findElement(By.xpath(".//div[text() = 'Customize']")).click();
Or As I'm seeing in HTML element has id attribute, if it's unique I'd to locate desire element and it's not being changed dynamically, you can try also using By.id() as below :-
webDriver().findElement(By.id("zcs1_repeatDesc")).click();
Edited :- If you want to click using JavascriptExecutor try as below :-
((JavascriptExecutor)driver).executeScript("arguments[0].click()", webDriver().findElement(By.xpath(".//div[text() = 'Customize']")));

Trouble locating a checkbox in an iframe with Selenium Web Driver

I'm trying to locate a checkbox in an iframe in a Javascript based website using Web Driver and python. I've tried locating by ID and XPATH and neither seem to work since I must be looking in the wrong frame. The checkbox is visible on the page to the user and selenium IDE also seems to come up with the same answer as me, but it still results in 'NoSuchElementException: Message: u'The element could not be found'
html for checkbox: input type="checkbox" onclick="CE.CESECUR.onClickFullSecurity()" id="cefullsecure"
selenium code:
_settings_ssl_locator = (By.XPATH, ".//*[#id='cefullsecure' and onclick='CE.CESECUR.onClickFullSecurity()']")
def click_settings_enable_ssl(self):
self.selenium.find_element(*self._settings_ssl_locator).select()
I think you you would need to switch to the frame first and then locate the element, do the task/action on the elements present on the frame and switch back to the previous frame.
WebElement checkBoxframe = driver.findElement(By.tagName("enter frame name here"));
driver.switchTo().frame(checkBoxframe);
//write your selenium code here for checkbox
driver.switchTo().defaultContent();