Unable to locate element using Selenium Python find element by xpath - python

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

Related

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

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

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')]")))

Confirm whether to write comments with xpath

Could you make an example with the site? I want to ignore the posts with comments and just click the ones without comments.
from selenium import webdriver
import time
path = "C:\chromed\chromedriver.exe"
driver = webdriver.Chrome(path) #path
'''
'''
driver.get("http://cineaste.co.kr/") #url
time.sleep(0.5)
postclick = driver.find_element_by_xpath("//h3[.='영화이야기']/following::div[#class='widget-small-box'][1]//li[#class='ellipsis'][not(contains(.,'+'))]") #로그인창 활성화
postclick.click()
driver.close()
I tried this, but there was an error.
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//h3[.='영화이야기']/following::div[#class='widget-small-box'][1]//li[#class='ellipsis'][not(contains(.,'+'))]"}
(Session info: chrome=81.0.4044.138)
I want to click on a post that doesn't have any comments yet. And I want to skip posts with comments.
I'm still a beginner. Help me.
The Element you are looking for is not available.
You need to recreate the Xpath. The Xpath you created is incorrect. See following image :-
You'll need these imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Example with the first entry with no comments in the "movie-reviews" block :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h3[.='영화감상평']/following::div[#class='widget-small-box'][1]//li[#class='ellipsis'][not(contains(.,'+'))][1]"))).click()

Filling webform with selenium, how to click button that is only enabled for certain conditions

I am trying to automatically fill in the form in this website using selenium with python:
https://breast.predict.nhs.uk/tool
I can fill in all the boxes except the "micrometastases only". If I try to click this one in the way I've filled in the others, i.e.:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
elem = driver.find_element_by_xpath("//*[#id=\"app\"]/div/div/div[3]/div/div/div/div/div[2]/form/div[4]/div[2]/div/div/div[3]/div/div/div/div/button[1]")
elem.click()
I get the following error:
selenium.common.exceptions.NoSuchElementException: Message: no such element:
Unable to locate element: {"method":"xpath","selector":"//*[#id="app"]/div/div/div[3]
/div/div/div/div/div[2]/form/div[4]/div[2]/div/div/div[3]/div/div/div/div/button[1]"}
I believe this is because the micrometastases button only becomes available if/when positive nodes = 1.
In order to try to fix this, I looked at using explicit waiting, in the following way:
element = WebDriverWait(driver, 20).until(ec.element_to_be_clickable((By.XPATH, "//*[#id=\"app\"]/div/div/div[3]/div/div/div/div/div[2]/form/div[4]/div[2]/div/div/div[3]/div/div/div/div/button[1]")))
element.click()
This outputs the following error:
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
What would be the right way to click this button? Any help is appreciated.
Thanks!
Use WebDriverWait and element_to_be_clickable() and following xpath option.
driver.get("https://breast.predict.nhs.uk/tool")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,"nodes"))).send_keys('1')
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[#class='btn btn-default btn-sm custom' and text()='Yes']"))).click()
Apologies, the button only becomes enabled after filling in the previous value and clicking somewhere else on the website. After changing that it worked perfectly.

Categories