Reputation: 244
I understand my question is really common. However, despite my vigorous search on Google and SO, I have been unable to find anything to help me with my problem with Python Selenium with the above error message "Unable to locate element". Almost all the threads I visited are about switching frames. I am unable to find any kind of frame tags on my site.
The problem is I am trying to select an input field so that I could send_keys but selenium is unable to locate the element. The element has the following XPath:
'//*[@id="app-root"]/div/div[2]/div/div[1]/div[2]/div[2]/div[1]/div[2]/div/input'
The following are the codes that I have:
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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('./chromedriver') # Optional argument, if not specified will search path.
driver.get(site_url);
time.sleep(5)
delivery_number = driver.find_element_by_xpath('//*[@id="app-root"]/div/div[2]/div/div[1]/div[2]/div[2]/div[1]/div[2]/div/input')
driver.quit()
I am unable to select the element with the above quote. xpath is unlikely to have an error as it is copied from chrome dev tool.
I tried to use this before the find_element line to no avail too:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="app-root"]/div/div[2]/div/div[1]/div[2]/div[2]/div[1]/div[2]/div/input')))
I apologise in advance for using a screenshot as page source does not accurately represent the page but please see screenshot of elements below.
This is the page source if it helps:
<!DOCTYPE html>
<html>
<head lang="zh-cn">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Nestia F&B</title>
<meta name="renderer" content="webkit">
<!-- No Baidu Siteapp-->
<meta http-equiv="Cache-Control" content="no-siteapp"/>
<meta name="apple-mobile-web-app-title" content=""/>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="stylesheet" href="/static/css/ui/amazeui.min.css"/>
<link rel="stylesheet" href="/static/css/app.css"/>
<link rel="stylesheet" href="/static/css/patch.css"/>
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/css/videoplayer.css">
<link rel="stylesheet" href="/static/css/movies.css">
</head>
<body>
<div id="app-root">
<div style="width:100%;text-align: center">
<img src="static/img/loading.gif"/>
</div>
</div>
<script src="/static/scripts/app.bundle.20200907180756.js"></script>
</body>
</html>
Thank you for reading till here. Some help will be much appreciated. :)
Upvotes: 0
Views: 660
Reputation: 33384
Use WebDriverWait
() and element_to_be_clickable
() and use either of the xpath
or css selector
.
XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//div[@class="am-form-group item-input"]/input')))
OR CSS selector
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.item-input>input')))
Upvotes: 2