Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

python - How to make textbox input in selenium with xpath

I made script that automatically picks a product from kith.com and go to the checkout:

element3 = driver.find_element_by_xpath('//*[@id="CheckoutData_BillingFirstName"]')


element3.send_keys("My First Name")

Kiths website:

<div class="col-sm-8 col-xs-12 fval">
                        <input class="form-control input-validation-error" data-val="true" data-val-countryaddressvalidation="" data-val-countryaddressvalidation-countryaddressvalidationpropname="" data-val-required="Billing First Name is required" data-val-unsupportedcharacters="Please use English characters only" data-val-unsupportedcharacters-unsupportedcharacterspattern="^[A-Za-z0-9,&quot;&quot;'`s@&amp;%$#*()[]._-s\/]*$" id="CheckoutData_BillingFirstName" maxlength="40" name="CheckoutData.BillingFirstName" placeholder="First Name" type="text" value=""><span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>
                    </div>

How can I locate the input form and send keys to it?


I am getting the same error every time :

Unable to locate element: {"method":"xpath","selector":"//*[@id="CheckoutData_BillingFirstName"]"}
question from:https://stackoverflow.com/questions/65836971/how-to-make-textbox-input-in-selenium-with-xpath

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your target element inside a <frame>:

<iframe src="https://fs.global-e.com/Checkout/v2/f77781eb-a7c0-43e2-822a-3ca96e8658f0?gaSesID=361925132.674171348.583&amp;gaMerchantClientInfo=undefined@undefined&amp;chkcuid=3ef950c0-4c7d-4cfe-bab5-3a1ed7035318&amp;isNECAllowed=true&amp;vph=631&amp;ift=87" class="Intrnl_CO_Container" id="Intrnl_CO_Container" name="Intrnl_CO_Container" allowtransparency="true" width="100%" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" height="1000px" style="height: 2196px;"></iframe>

You need to switch it first:

#after clicked checkout button

time.sleep(10)
driver.switch_to.frame(driver.find_element_by_id("Intrnl_CO_Container"))

time.sleep(10)
element3 = driver.find_element_by_xpath('//*[@id="CheckoutData_BillingFirstName"]')
element3.send_keys("My First Name")

But there is a better way to wait in selenium, for detail you can read the @pcalkins suggestion.

After clicked checkout button, you can add following code:

#after clicked checkout button

wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'Intrnl_CO_Container')))
element3 = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="CheckoutData_BillingFirstName"]'))).click()
element3.send_keys("My First Name")

Please import:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...