Selenium cannot find element even after giving a high implicitly waiting time - python

Im trying to automize some webpages and run into the following error:
I have passed the login screen, which gives no problems, but after being redirected to the next page, python gives the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
I have tried to fix it by introducing the driver.implicitly.wait, the time.sleep and the WebDriverWait(driver, time).until(EC.presence_of_element_located((By.ID, "myDynamicElement")) functions, but all the options do not seem to work.
I have shown the relevant HTML code below:
<input type="Text" class="urEdf2TxtEnbl" autocomplete="off" id="DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp" ct="I" name="DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp" st="" tp="STRING" value="502309" onchange="sapUrMapi_InputField_change('DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp',event);" onblur="sapUrMapi_InputField_Blur('DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp',event);" onkeydown="sapUrMapi_InputField_keydown('DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp',event);" onkeyup="sapUrMapi_InputField_KeyUp('DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp',event);" onfocus="sapUrMapi_InputField_focus('DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp',event);" onselectstart="sapUrMapi_InputField_onselectstart('DLG_VARIABLE_vsc_CommonVariablesList_VAR_5_VARINPUT_inp',event);">
The HTML code in the region around this is shown below:
<input type="Text" class="urEdf2TxtEnbl" autocomplete="off" id="DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp" ct="I" name="DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp" st="" tp="STRING" value="" onchange="sapUrMapi_InputField_change('DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp',event);" onblur="sapUrMapi_InputField_Blur('DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp',event);" onkeydown="sapUrMapi_InputField_keydown('DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp',event);" onkeyup="sapUrMapi_InputField_KeyUp('DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp',event);" onfocus="sapUrMapi_InputField_focus('DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp',event);" onselectstart="sapUrMapi_InputField_onselectstart('DLG_VARIABLE_vsc_CommonVariablesList_VAR_4_VARINPUT_inp',event);">
What I am trying to do is to change the value: value="502309". See below for the full code:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('xxxx')
login = driver.find_element_by_xpath('xxx')
login.send_keys('xxx')
passw = driver.find_element_by_xpath('xxx')
passw.send_keys('xxx')
button = driver.find_element_by_xpath('xxx')
button.click()
driver.maximize_window()
driver.implicitly_wait(120)
mat = driver.find_element_by_id('DLG_VARIABLE_vsc_CommonVariablesList_VAR_2_VARINPUT_inp')
driver.close()

Try to print the html using beautifulsoup so you can check if the element really exist. For your reference on how to use it, see link

I don't know if this will help but i had the same problem and solved it by this:
searchbox = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located(
(By.XPATH, "your_xpath_here")
The above code shows that the webdriver will wait's for an element located in your page.

Issue with your code is not wait , but rather correct element locator. As far as I can see from limited HTML DOM you have provided, part of name and id of element you want to locate inside variable mat is changing. You can locate your element using below code ( Also i have included more sophisticated way to wait for element load).
mat = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.XPATH, "//input[starts-with(#id,'DLG_VARIABLE_vsc_CommonVariablesList_VAR')]")))

Related

Unable to locate element using Selenium Python find element by xpath

I have input:
<input type="text" class="cell-danhgia" kpi-chitieu="001" id="txt-danhgia-001" value="0">
I use Selenium with python to select input. My code is:
button = driver.find_element(by=By.XPATH, value="//input[#id='txt-danhgia-001']")
u = '100'
button.send_keys(u)
But I can not locate input. I've got an error message:
NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#id='txt-danhgia-001']"}
(Session info: chrome=105.0.5195.127)
Please help to how to select this input. Thank you very much.
#Richard Nguyen, Since I do not know the url but may be the element is taking time to load. So it is good to wait for its visibility.
Try this:
button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[#id='txt-danhgia-001']")))
instead of :
button = driver.find_element(by=By.XPATH, value="//input[#id='txt-danhgia-001']")
in your code.
You will need the following import for it to work:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
If this does not work then other possibilities may be element is inside an iframe/frame. In that case, you need to first switch to the frame using driver.switch_to.frame(frametoswitchto)
Try this:
driver.find_element(By.ID, 'txt-danhgia-001')
You could also shorten your code to:
driver.find_element(By.ID, 'txt-danhgia-001').send_keys('100')

Python + Selenium: Unable to locate element

I'm trying to locate a tab and click it on my webpage. However, my code does not work:
ActionChains(driver).move_to_element(driver.find_element_by_xpath("//*[#id='tab_bd3ae39d-f956-49ab-b7bd-f13507de9351']/div[2]/div")).perform()
additionaldata_ele= driver.find_element_by_xpath("//*[#id='tab_bd3ae39d-f956-49ab-b7bd-f13507de9351']/div[2]/div").click()
The HTML body is as follows:
<li class="WJX1 WLV1" id="tab_015ba30c-af6c-4c9a-ac34-f77ee00805b6" role="tab" aria-controls="tabPanel_16845ddd-961b-4581-89da-a6a4e6080930" data-automation-id="tab" aria-selected="false"><div class="WGX1"></div><div class="WEX1"><div class="gwt-Label WLX1" data-automation-id="tabLabel">Additional Data</div></div></li>
I get the error -
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='tab_bd3ae39d-f956-49ab-b7bd-f13507de9351']/div[2]/div"}
I guess the reason is that when I try to find the element, it doesn't appear on the DOM, so I should implement WebDriverWait until the element is visible. So I tried WebDriverWait, but it didn't work either.
Many thanks for all answers and replies!
second edition:
Here is the webpage, sorry I cannot share the link, it is an internal webpage and PSW is required:
This is the screenshot of the page
That id looks dynamic - it will probably change frequently and result in an unstable script.
Additionally, you will want a wait to ensure your web page is ready before you continue.
try something like this:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[text()="Additional Data"]'))).click()

Selenium no such element: Unable to locate element (trying to login)

I am trying to use Selenium to download a file from https://id.opswat.com, in order to access it I need to login, but I can't, because when I try to retrieve the email input, it gives me an error ... This is my code:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://id.opswat.com/login')
driver.find_element_by_name('email').send_keys('email#email.es')
driver.find_element_by_class_name('form--button is-primary is-fullwidth button').click()
driver.find_element_by_name('password').send_keys('pass')
driver.find_element_by_class_name('form--button is-primary is-fullwidth button').click()
And this is the error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="email"]"}
The login on that page is a bit special, because first you have to enter the email, check that it is well formed, click to continue, and you will see the password entry and the button to log in ...
Maybe something very basic is missing me, but I can't find it ... (I've tried waits, but neither ...)
First, your line
driver.find_element_by_class_name('form--button is-primary is-fullwidth button').click()
will always fail because class_name function does not handle spaces in the class. I'd suggest changing that to
driver.find_element_by_css_selector('.form--button.is-primary.is-fullwidth.button').click()
Next, You need to wait for the page to load before you try to interact with each field. This works for me:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get('https://id.opswat.com/login')
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.NAME, 'email'))).send_keys('email#email.es')
driver.find_element_by_css_selector('.form--button.is-primary.is-fullwidth.button').click()
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.NAME, 'password'))).send_keys('pass')
driver.find_element_by_css_selector('.form--button.is-primary.is-fullwidth.button').click()
I can't see anything obviously wrong with it, have you checked if you can get the driver.find_element_by_class_name('emailInputBox')? Or any other element in the page?
Does it work when getting by XPath?
//*[#id="root"]/div/section/div/div/div[2]/form/div[2]/div/div/div/input
You could also try waiting for the element to be present like in this answer:https://stackoverflow.com/a/50324722/10832847

Send keys to HTML element using Selenium

I am writing an automation script for sports betting in Python using Selenium. I am stuck at a point where Selenium is unable to click or send keys to the specific HTML element highlighted in the following screenshot (https://i.stack.imgur.com/NbljY.png).
Here is what I have tried:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://www.bet365.com")
### Some code here to navigate to a particular match
driver.switch_to.frame(bet_driver.find_element_by_tag_name("iframe"))
elem = driver.find_element_by_class_name("bs-Stake")
elem.click()
elem.send_keys("100")
This returns the following error:
ElementNotInteractableException: Element <div class="bs-Stake"> is not reachable by keyboard
If I try instead
elem = driver.find_element_by_class_name("stk bs-Stake_TextBox")
I get the error:
NoSuchElementException: Unable to locate element: .stk bs-Stake_TextBox
I would appreciate help navigating to the HTML element, clicking and sending keys to it, using any method available in Selenium.
Try to sendkeys with webdriver wait so element would be able to interact.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 50).until(EC.visibility_of_element_located((CSS_SELECTOR, ".stk.bs-Stake_TextBox")))
element.send_keys("100")
Although CSS styles are inherited... the class designations are not. Your css selector has three separate classes in it, but your element sample only has one.
Selenium is designed to mimic the same actions that a person can perform... the error suggests that the field is not available for interaction.... find out why.

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