Why am I receiving an ElementClickInterceptedException error? - python

The question on the survey asks: What days of the week are you consistently available? I would like to check Sunday.
I am duplicating the code I see from an online video, however, I am receiving this error. Some people are suggesting that a pop-up may be blocking the program from working correctly but I do not see a pop up.
I have tried using chromedriver and geckodriver. The error is present in both.
The code to see if Sunday is selected works properly:
status=driver.find_element_by_id("RESULT_CheckBox-8_0").is_selected()
print(status)
output:
False
Here is my code:
from selenium import webdriver
driver=webdriver.Chrome(executable_path="my_webdriver_path"\\chromedriver.exe
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407")
What days of the week are you consistently available?
I would now like to check the Sunday box. Here is my code:
status=driver.find_element_by_id("RESULT_CheckBox-8_0").click()
print(status)
I would like the Sunday box checked but I am receiving this error:
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <input id="RESULT_CheckBox-8_0" class="multiple_choice" name="RESULT_CheckBox-8" type="checkbox"> is not clickable at point (313,599) because another element <label> obscures it
I do not see another element that obscures the program. Does anyone have any suggestions? I am new to coding so any help would be greatly appreciated.

The problem you're having is related to the same values that are being used for different attributes in the label and input tags.
As you can see the 'for' attribute of the label has the same value as the 'id' attribute (wich doesn't allways have to be a unique value) of the eventhough you are using 'find_element_by_id'.
To fix this you could use a different locator like XPATH. You can get a xpath by right clicking on the element (when you are inspecting the code with f12) and than select [copy] - [xpath]
Here is some code that should work (note: I have placed the chromedriver.exe in the same location as the .py file it self):
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407')
status = driver.find_element_by_id("RESULT_CheckBox-8_0").is_selected()
if status:
pass
else:
driver.find_element_by_xpath("//*[#id='q15']/table/tbody/tr/td[1]/label").click()
Don't forget to eventually close the driver, otherwise it will stay in de background as a running procces.
You can do so by:
driver.close()
Hope this helps!

Try use ActionChains to click the element.
element = driver.find_element_by_id("RESULT_CheckBox-8_0")
ActionChains(driver).move_to_element(element).click(element).perform()
status = driver.find_element_by_id("RESULT_CheckBox-8_0").is_selected()
print(status)
Following import:
from selenium.webdriver import ActionChains

To click() on the checkbox with text as Sunday you can use the following Locator Strategy:
Using XPATH:
driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[starts-with(#for, 'RESULT_CheckBox-') and contains(., 'Sunday')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:

Related

How to clear "selenium.common.exceptions.ElementNotInteractableException" error?

I am a newbie to python so please excuse me. I have no background in programming, I am just trying things to smooth out something for me by automating.
I found other similar questions but my problem still persists after following some of the solutions posted.
I am trying to automate uploading files to a website, the website allows one file for upload at once, so I am trying to automate uploading the task of uploading one by one.
I am using selenium and I have progressed to logging in the page and attaching the file to upload. Now I am stuck as the website requires 3 tags to be inputted. There is one long field to input tags and is backed up by auto suggestion/complete. Cannot enter custom words, only those provided by website which is in auto suggestion.
The above error is displayed when I try to sendkeys() to that tag field element. Below is that element
[https://imgur.com/a/VGq7AtY]
I have tried Implicit wait and explicit wait as well but it doesn't work out, as seen below
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get('https://www.website.com/login')
driver.find_element_by_xpath('//*[#id="app"]/div[1]/form/div[1]/div[2]/div[1]/input').send_keys('username')
driver.find_element_by_xpath('//*[#id="app"]/div[1]/form/div[1]/div[3]/div[1]/input').send_keys('password')
driver.find_element_by_xpath('//*[#id="app"]/div[1]/form/div[2]/button').click()
driver.find_element_by_xpath('/html/body/div/header/div[1]/span[2]').click() // upload page
driver.find_element_by_xpath('//*[#id="app"]/div[1]/div/div[1]/div[1]/div[1]/div[1]/label/input').send_keys('/home/userrobin/Desktop/file.html') // file to be uploaded path
time.sleep(10)
driver.implicitly_wait(40)
tags = WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="tags-selector"]')))
# tags = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '//*[#id="tags-selector"]')))
tags.send_keys('assignment')
Error : selenium.common.exceptions.ElementNotInteractableException: Message: Element is not reachable by keyboard
What am I missing, what can I do to identify the problem?
Thank you
EDIT
added
I tried with
`
(By.XPATH, '//input[#id="tags-selector"]')
` , it gives is invalid: SyntaxError: Document.evaluate: The expression is not a legal expression Stacktrace: , I am not sure on how to refine locator to include //input
tried
driver.find_element_by_xpath('//*[#id="tags-selector"]/input').send_keys('assignment')
but it gave selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //[#id="tags-selector"]/input*
tried
driver.find_element_by_xpath('//*[#id="tags-selector"]').click()
this works on that field as now I can see the cursor in the field.
but when I try send_keys(), it shows me error not reachable by keyboard

selenium.common.exceptions.NoSuchElementException error sending text to input fields using Selenium and Python

I'm trying to write a simple program to fill out a form (including order ID and zip code) to be submitted but I keep getting the following error:
selenium.common.exceptions.NoSuchElementException: Message: " (without any text following "Message
Code trials:
from selenium import webdriver
browser = webdriver.Safari()
browser.get('https://knowledge.tonal.com/s/order-status')
orderElm = browser.find_element_by_id('input-3')
orderElm.send_keys('1000XXX')
zipcodeElm = browser.find_element_by_id('input-4')
zipcodeElm.send_keys('90210')
zipcodeElm.submit()
I've double-checked my element ID several times and though I'm very new to this, I'm fairly confident I have the correct element IDs. What am I doing incorrectly?
To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH:
driver.get("https://knowledge.tonal.com/s/order-status")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#data-aura-class='cOrderSearch']//following::input[1]"))).send_keys('1000XXX')
driver.find_element(By.XPATH, "//div[#data-aura-class='cOrderSearch']//following::input[2]").send_keys("90210")
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
There are mutliple issues here:
A. After getting the url, you are not waiting for the elements in the page to load completely and hence the elements are not found
B. The locators you have seem dynamic to me, for eg: input-3, I see it as input-5 (although I am on Chrome browser, but that may not rule out that the locators are dynamic). So, I refactored and hunted for some static locator strategies which I pasted below.
C. zipcodeElm.submit() would not work, as it is not the button element. I have refactored this too.
So, here is the code.
driver.get('https://knowledge.tonal.com/s/order-status')
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "(//div[#data-aura-class='cOrderSearch']//input)[1]")))
orderElm = driver.find_element(By.XPATH, "(//div[#data-aura-class='cOrderSearch']//input)[1]")
orderElm.send_keys('1000XXX')
zipcodeElm = driver.find_element(By.XPATH, "(//div[#data-aura-class='cOrderSearch']//input)[2]")
zipcodeElm.send_keys('90210')
driver.find_element(By.XPATH, "//*[#data-aura-class='cOrderSearch']//parent::div//button").click()
Output: (exit code 0 implies that the code passed without errors)
Process finished with exit code 0

Why am I getting the message that the search-bar is not intractable?

The code is supposed to type "fish" into the YouTube search bar using Selenium and a Chrome Browser.
I have tried the xpaths of mulitple divs that hold the tag and they didn't work either.(not sure if the error was the same though) The xpath in the code is for the <input> tag so it should be fine.
I also watched a tutorial and the xpath was exactly the same so that shouldn't be the problem since it worked for the YouTuber.
It also took me some time to figure out that the find_element_by_* are depreciated functions.
Could it be that the .send_keys has also been changed? I did try to find the selenium changes in 4.1.0 and it said nothing about it on a website that I found.
Should I maybe delete Selenium 4.1.0 and install an older version? For simplicity sake. Since there is probably a bigger number of tutorials for it.
from selenium import webdriver
from selenium.webdriver.common.by import By
setting = webdriver.ChromeOptions()
setting.add_argument("--incognito")
# I open the browser in incognito just so I don't clutter my search
# history with dumb stuff as I'm testing things out
# could it be a part of the problem?
driver = webdriver.Chrome(options = setting)
driver.get('http://youtube.com')
searchbox = driver.find_element(By.XPATH, '//*[#id="search"]')
searchbox.send_keys('fish')
Error Message:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
wait=WebDriverWait(driver,60)
driver.get('http://youtube.com')
searchbox = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input#search")))
searchbox.send_keys('fish')
In order to send_keys to that element wait for it to interactable and then send keys.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Outputs:

How to retrieve the Ping Download and Upload time from browser tests?

I am trying to automate speedtests with different browsers automatically, and the main part of the test is inside a loop. The problem is, sometimes, one element which has been selected before, and the script worked correctly, at the one of the next steps, exactly at the same loop and at the same page, but with different number, without any change in the xpath, selenium cannot select it again. So, I can not repeat my test as much as I want.
Most of the time I have this problem with Edge, and I think one reason can be, the xpath for elements which I found by help of Chrome or Firefox. ( I can not find the xpath in Edge first of all, I searched a lot about it).
I also put the different xpath that I use. Actually I want to get the numeric or string values of ping,download, upload location and server.
Please let me know, how can I solve this issue, I tried different sleep time and two different xpath. the script always gives me error when I am trying to select the element with class_name or css_selector.
firefox:
"/html/body/div[3]/div[2]/div/div/div/div[3]/div[1]/div[3]/div/div[3]/div/div[1]/div[2]/div[1]/div/div[2]/span"
chrome:
"//[#id='container']/div[2]/div/div/div/div[3]/div[1]/div[3]/div/div[3]/div/div[1]/div[2]/div[1]/div/div[2]/span"
chrome:
"//div[#class='result-item result-item-ping updated']/div[2]/span"
Other question is how can I wait for a page to load completely. this method WebDriverWait(driver,some seconds) does not work for me and i have to use time.sleep()
Error:
selenium.common.exceptions.NoSuchElementException: Message: No such element
element = driver.find_element_by_xpath("/html/body/div[3]/div[2]/div/div/div/div[3]/div[1]/div[3]/div/div[3]/div/div[1]/div[2]/div[1]/div/div[2]/span")
To automate the speedtests you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Edge(executable_path=r'C:\WebDrivers\MicrosoftWebDriver.exe')
driver.get("https://www.speedtest.net/")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.js-start-test.test-mode-multi"))).click()
WebDriverWait(driver, 45).until(EC.url_contains("result"))
print("Ping :"+driver.find_element_by_css_selector("div[title='Reaction Time'] div.result-data.u-align-left>span").get_attribute("innerHTML"))
print("Download: "+driver.find_element_by_css_selector("div[title='Receiving Time'] div.result-data.u-align-left>span").get_attribute("innerHTML"))
print("Upload :"+driver.find_element_by_css_selector("div[title='Sending Time'] div.result-data.u-align-left>span").get_attribute("innerHTML"))
#driver.quit()
Console Output:
Ping :35
Download: 21.53
Upload :3.46
Browser Snapshot:
Use the following CSS locators to identify the values:
Download: *.result-data-large.number.result-data-value.download-speed*
Upload: *.result-data-large.number.result-data-value.upload-speed*
Ping: *.result-data-large.number.result-data-value.ping-speed*
Making use of getText(), you can retrieve their values. Wait for an element in the page to be visible to make sure the page is loaded successfully.
Try with:element = driver.find_element_by_xpath("/html/body/div[3]/div[2]/div/div/div/div[3]/div[1]/div[3]/div/div[3]/div/div[1]/div[2]/div[1]/div/div[2]/")
Maybe also you need to catch exception for: NoSuchElementException cases.
I've tested these CSS selectors and they work in both Chrome and Edge.
span.ping-speed # ping
span.download-speed # download
span.upload-speed # upload
div.server-current > div.result-label # server
If you want to know when the page is done loading, you can wait until the URL changes from https://www.speedtest.net to https://www.speedtest.net/results/<some number>. I would just use WebDriverWait and url_contains("results") , e.g.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.url_contains("results"))
There are some other approaches in this question.
WebDriverWait driverWait = new WebDriverWait(driver, 30000);
driver.get("https://www.speedtest.net/");
WebElement goLink = driver.findElement(By.cssSelector(".js-start-test.test-mode-multi"));
driverWait.until(ExpectedConditions.elementToBeClickable(goLink));
goLink.click();
By download = By.cssSelector(".result-data-large.number.result-data-value.download-speed");
By upload = By.cssSelector(".result-data-large.number.result-data-value.upload-speed");
By ping = By.cssSelector(".result-data-large.number.result-data-value.ping-speed");
driverWait.until(ExpectedConditions.urlMatches("https://www.speedtest.net/result/[0-9]"));
String downloadSpeed = driver.findElement(download).getText();
String uploadSpeed = driver.findElement(upload).getText();
String pingValue = driver.findElement(ping).getText();
System.out.println("Download: "+downloadSpeed + "\nUpload: "+ uploadSpeed + "\n Ping: "+pingValue);
Output
Download: 78.82
Upload: 45.93
Ping: 23

Element is not visible with Splinter (Selenium)

After filling a form I am trying to click the following submit button:
<div class="tl_formbody_submit">
<div class="tl_submit_container">
<input type="submit" class="tl_submit" value="Einstellungen speichern" onclick="Backend.getScrollOffset()">
</div>
The code is:
browser.find_by_xpath('//input[#class="tl_submit"]').click()
The stacktrace says, that the element is not visible - but why?
Thanks!
# import options
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
# desired url link
URL = 'www.path.com'
#driver & settings
driver = webdriver.Chrome('PATH TO CHROMEDRIVER.EXE')
driver.get('URL')
driver.delete_all_cookies
#find element & click
driver.find_element_by_id("Einstellungen speichern").click()
if the stacktrace says element is not visible, if means it's not visible when you are trying to find it.
may be some action requires to make it visible (some ajax request) or may be you just need to wait a while before you click it.
you could use explicit wait as mentioned below.
submit_button = WebDriverWait(driver, 30).until(EC.visibility_of_any_elements_located((By.XPATH, "//input[#class='tl_submit']")))
submit_button.click()
One of the reason for the element to be 'not visible' is due to the error in assigning the xpath. Best way is to copy the xpath from the html (inspect element) and paste it. It works for most of the cases, try it.
You can check the link below, I had got a similar problem too, the possible solution can be here but for me the above-mentioned solution was fine.
How to fix "Element not interactable" Selenium error in Python?

Categories