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
849 views
in Technique[技术] by (71.8m points)

python-3.x - Selenium无法正确加载html-Python3.8(Selenium does not load html properly - Python3.8)

i'm using selenium with python to register into a website.

(我正在使用硒与python一起注册到网站。)

My problem is that when i need to insert the credit card number and the security number, though i copy the xpath of the field, wait for the field to be visible and clickable, python raise a timeout exception becouse he didn't find any field with that xpath.

(我的问题是,当我需要插入信用卡号和安全号时,尽管我复制了字段的xpath,等待该字段可见且可点击,但是python引发了超时异常,因为他没有找到任何字段那个xpath。)

The html code of the field is that:

(该字段的html代码是:)

<input id="cardnumber" autocomplete="cc-number" type="tel" pattern="[0-9]*" placeholder="1111 2222 3333 4444" data-encrypted-name="number" class="form-control">

UPDATE: Maybe I understand.

(更新:也许我明白。)

When I print out driver.page_source I notice that it's not loaded properly, and now I understand why Selenium can't find nothing, neither by xpath, neither by name or everything.

(当我打印出driver.page_source我注意到它没有正确加载,现在我明白了为什么Selenium找不到任何东西,无论是通过xpath,通过名称还是全部都找不到。)

I notice that what is loaded can be clicked and all, but some fields are not loaded.

(我注意到可以单击加载的全部内容,但是某些字段未加载。)

So why Selenium behave like that?

(那么,硒为何如此表现呢?)

UPDATE AGAIN: Solved, the solution is that i need to switch iframe every time.

(再次更新:解决了,解决方法是我每次都需要切换iframe。)

  ask by Gianla translate from so

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

1 Answer

0 votes
by (71.8m points)

Try This By Adding Some Wait:

(尝试添加一些等待时间:)

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



driver = webdriver.Chrome()
driver.maximize_window()
driver.get("Your Website")
try:
    cardnumber = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "#cardnumber")))
except:
    print('Sorry!')


cardnumber.send_keys('XXXXX')

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

...