Dan Brittle
Dan Brittle

Reputation: 33

Why can I find this text box element in selenium?

Hi I cant get my selenium code to find this element in a page, it appears after ive used code to click a button on a page which opens up a form under the button.

Im looking to try get it to click on this input text box

<input id="number" class="iField" type="text" name="number">

My code

driver1.implicitly_wait(10)
driver1.until(EC.visibility_of_element_located(By.CSS_SELECTOR("input#number")))
button = driver1.find_element_by_xpath('//*[@id="number"]')

Website code

<form id="form" action="https://secureacceptance.cybersource.com/silent/embedded" method="post" autocomplete="off">
<div class="form-row">
                    <div class="left-col">
                        <label>No: </label>
                    </div>
                    <div class="right-col">
                        <input id="number" class="iField" type="text" name="number">
                    </div>
                </div>

I've tried using Xpath, name Etc, but keep getting the unable to locate element.

Edit ive also tried this

num = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located(By.XPATH("//*[@id='number']")))
num.click()

But get this error

Traceback (most recent call last): File "c:\Users\OneDrive\coding\code.py", line 68, in num = WebDriverWait(driver1, 10).until(EC.visibility_of_element_located(By.XPATH("//*[@id='number']"))) TypeError: 'str' object is not callable

Upvotes: 1

Views: 595

Answers (2)

PDHide
PDHide

Reputation: 19989

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

num = WebDriverWait(driver1, 10).until(
    EC.visibility_of_element_located((By.XPATH,"//*[@id='number']")))

your webdriverwait syntax is incorrect use the above one , you should not call By.xpath but pass it as a tuple to the EC

also do not use implicit wait ,

Upvotes: 0

vitaliis
vitaliis

Reputation: 4212

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By 
 
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#number")))
element = driver.find_element_by_css_selector("#number")
element.click()

You have id, so you can also use find_element_by_id instead of find_element_by_css_selector:

element = driver.find_element_by_id("number")
element.click()

Please note that I am waiting till this element becomes actually clickable.

Also, I don't understand why you are using driver1. Use driver for all elements.

Try 2 The answer is easier than I thought. You are messing up with ()

num = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='number']")))

Try 3 (more detailed)

wait = WebDriverWait(driver,10)

wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@id='number']")))

Check here how to use explicit waits https://selenium-python.readthedocs.io/getting-started.html

Upvotes: 0

Related Questions