Selenium is not able to find the element even though it exist - selenium

It is the line of code that i am using to find the element using chrome web driver and python
This the error that is thrown everytime when i try to run the code
These are the buttons on the header bar and I am trying that my code should click the html button
This is the html code for the efforts button which i am trying to target using selenium so that it can click here but it always says no such element found
I have even created a seperate function to again and again find the element and sleep for 5 sec after every successful attempt until it has clicked on the element but still no luck it runs on forever without ever finding the element
This is where i am calling the function explained in the 5th picture. Here driver is chromedriver and it keeps on repeating for 10 times with 5 sec intervals but still no luck and i am continuously watching the chrome window where driver do all the work and the page has loaded but even after it is loaded it is still not working.
It is the beginning of entire html code from inspect element in case needed by someone
This is the remaining HTML code with EFFORTS area selected in case needed
I have even tried using shadow root but that is also not working and i am also not sure that this is a case of shadow root as i have not seen shadow root specified in html when i checked the html code.

The element you are trying to locate is under iframe with class iframeStyle. First switch to that iframe to access the Efforts link.

try below xpath using xpath contains to resolve your issue:
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'EFFORTS')]")))
or
WebDriverWait(driver, 30).until(
EC.element_to_be_clickable((By.ID, "tmsMobileId")))
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
If you still facing no such element exception then please check if your element is present under iframe. If so then you need to switch iframe before handling EFFORTS anchor tag

Related

Selenium clickable element clicked at wrong position?

We are waiting for a button identified by xpath to be clickable with:
ExpectedConditions.elementToBeClickable()
and then execute click on this button in Selenium.
We then get an error that:
another element would receive the click at position (x, y).
It is possible that this button is moving around slightly on the page during page loading as other buttons next to it are loading.
Why would Selenium report that the button is clickable and then not be able to click it? I understand that's what this condition is for. This execution happens in the same line.
How could we solve this problem?
Imagine that you want to interact with a button and that button is in middle of the page.
Case1:
You do not open the browser in full-screen mode through an automation script.
Then its position will not be in the middle of the page.
you can solve this issue by just introducing
driver.maximize_window()
Case2:
Maybe in the automation script, some pop up or ads shows up and that really hides the button in the background. In this case, you will likely encounter another element would receive the click at position (x, y).
Solution: You should determine what kind of pop up/Alert/ads they are and handle them separately then only you should interact with the intended button.
Case3:
It could also occur cause you might have open (let's say calendar to select a start/end date) and then because of that web elements that are just under the calendar view won't be accessible to selenium and only the Calendar view will get the click. [This should answer your query]
In General, I use actions class or JS to inject js code in the selenium script.
If you are using Selenium with Python then you can use
Use ActionChains:
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "CSS here")))).click().perform()
Use execute_script:
wait = WebDriverWait(driver, 20)
button= wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "CSS here")))
driver.execute_script("arguments[0].click();", button)
In Python you'd need the below imports as well:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
Using ExpectedConditions.elementToBeClickable() allows our code to halt program execution, or freeze the thread, until the condition we pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting. Thus Explicit waits allows us to wait for a condition to occur which inturn synchronises the state between the browser and its DOM Tree, and the WebDriver script.
Even with ExpectedConditions.elementToBeClickable() there can be instances while invoking click() on the desired element you may face either of the following errors:
Element ... is not clickable at point (415, 697). Other element would receive the click: <div class="cc-content">...</div>
or
WebDriverException: Element is not clickable at point (36, 72). Other element would receive the click
Reason and Solution
There can be numerous reasons for this error to occur and a couple of them and their remediation are as follows:
Though the element is within the Viewport but may be hidden behind the cookie banner. In such cases you have to accept the cookie consent before you interact with the desired element as follows:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("cookieConsentXpath"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("elementXpath"))).click();
The element may be clickable but behind a loader. In such cases you have to wait for the loader to disappear before you interact with the desired element as follows:
new WebDriverWait(driver, 20).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("loader_cssSelector")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("element_cssSelector"))).click();

Can not find by xpath until inspect the element

I was using Selenium find_element_by_xpath to click the cookie popup "Accept" button. But it was failing to find the button. So, I tried to locate the button using querySelector in the dev console. Also, the querySelector failed to find the button but after clicking inspect on the button the querySelector able to find the button.
Also, searching the xpath in dev elements just showing 1 of 1.
Why this is happening ? How do I click the "Accept" button using selenium ?
Website link: https://www.transfermarkt.com/
The xpath: //*[#id="notice"]/div[3]/div[2]/button
After inspect on the button.
That element is inside an iframe, so to access it you will first have to switch to that iframe.
You didn't mention what language are you using so I will give my solution in Python. This can be done with other languages as well with some little syntax changes.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,'iframe[title="SP Consent Message"]')))
wait.until(EC.visibility_of_element_located((By.XPATH, '//button[#title="ACCEPT ALL"]'))).click()
When finished working inside this iframe you will have to switch back to the default content with
driver.switch_to.default_content()

Element not interacable

I have this error "element not interactable" When I try to click a drop down list with selenium. However in debug mode, when I inspect ( press F12 ) before the break points and I continue to run then the test is passed. So my question is Why the elements can be clicked and what should I do to prevent the situation again. Many thanks!
You have to add wait / delay before accessing the elements to let the elements be fully loaded on the page before accessing them.
The simplest way is to add hardcoded sleep, like
time.sleep(5)
The better way is to use explicit way implemented by expected conditions.
Few things to note down,
Always launch browser in full screen mode.
driver.maximize_window()
this code should be written before driver.get()
Whenever you get element not interactable, try to use action chains to click or send_keys :
something like this :-
from selenium.webdriver.common.action_chains import ActionChains
action = ActionChains(driver)
action.move_to_element('your web element here').click().perform()
Make sure that using Selenium, the desired web element should be in Selenium view port.
in order to resolve this, you may have to scroll down to let Selenium know where exactly the element is.

Finding xpath of an element that appears for a very short amount of time

Is there a good method to find the xpath of an element that only appears very briefly (e.g. for a second or two, like a loading screen).
If I click within an application and a short loading screen appears I would like to find the xpath of the loading element so I can have selenium wait until the element is no longer on the page before continuing.
Sometimes the loading screen appears over the element I want to click and catches the click instead.
Thanks!
Assuming that you are struggling to find XPath of the element that quickly disappears from the page before you could inspect and find the xpath.
You can try opening the console in the webpage by using Ctrl+Shift+I.
Then navigate to the Network tab next to console tab
Below Network tab you will find Online dropdown.
Click on the arrow beside the online dropdown, you will find multiple options click on the one which says Slowest This will reduce the speed of your website loading and gain you more time to find the xpath.
or
You can also customize the throttle by clicking on add option and providing Download, Upload and Latency. or you can directly choose Offline option once your loading icon is enabled.
Attached screenshot for your reference.
Hope this helps.
You can try something like below using web driver wait, not sure which programing language you are using butyou refer same concept with other languages too:
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'your text1')]")))
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

Selenium “Element is not clickable at point” error in Firefox but working in Chrome

In Selenium I am trying to locate an element.
But getting the below error:
org.openqa.selenium.WebDriverException: Element is not clickable at point (1009.25, 448.183349609375). Other element would receive the click: <rect data-sdf-index="7" height="390" width="420" class="aw-relations-noeditable-area"></rect> (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 12 milliseconds
Getting this error in firefox. But its working successfully in Chrome browser.
Is anyone having solution for it?
I already tried help from this post:-Selenium "Element is not clickable at point" error in Firefox but not able to get the result.
I have written below code:
public void createPortOnSelectedNode( String nodeName ) {
ISingleLocator m_nodeContainer = m_nodePage.getNodeContainer();
WebElement node = m_nodePage.getNode( m_nodeContainer, nodeName ).getElement();
Actions action = new Actions(DefaultDriver.getWebDriver());
action.moveToElement(node, 40, 0);
action.click();
action.perform();
}
Hi the above error comes under such scenario where Your webdriver script performs the action but the element on which you want to do operation is not properly loaded inside the DOM i.e its position is not fixed inside the DOM tree (also note selenium is able to perform its action because element is available inside the DOM hence webdriver only looks for the presence of element inside the DOM and not its position inside the DOM)
So how to overcome this issue
1.Give time to DOM to properly give positions to its element.
and that can be achieved by :
1.Instead of performing operation's directly at the target area try to do some extra/false
activity with webdriver which will give time for DOM to position all of his elements
2.apply Thread.sleep().
3. also if you are running your test in smaller window size then set the size to maximum it
will also help
i have not included any code cause the link that you have refer in the question contains ample amount of work regarding that so i decided to make everybody underrated why this error occurs. thanks hope this helps
Have you tried to click directly using Javascript? In python I use
driver.execute_script("arguments[0].click();", elt)
In Java it should look like executeScript instead...