0

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.

enter image description here

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. :)

Cadell Teng
  • 224
  • 3
  • 23

1 Answers1

2

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')))
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Hi @KunduK, Thank you for your answer. The programming is no longer showing an error that it cannot find the element. However, I have an issue and that is I tried send_keys but nothing was sent. The field remains empty. Below's my code: delivery_number = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//div[@class="am-form-group item-input"]/input'))) deliveryyy_number.send_keys("Hello") time.sleep(5) driver.quit() Program ends after running last line. There is no error message it just ends. – Cadell Teng Sep 09 '20 at 14:41
  • Hi @KunduK, thank you for sharing the answer. The program appears to be able to select the item now. But send_keys appears to be not working as when I did send_keys, nothing was sent to that element. Do you have any idea? There is no error message, program appears to just skip over that line of codes. – Cadell Teng Sep 09 '20 at 15:43
  • You need to share your url for further investigation? – KunduK Sep 09 '20 at 18:31