为什么python selenium有时发送密钥而不输入我的密钥?

tct7dpnv  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(457)

我正试图给一家汽车gmail制造商编码,我正在确认电话号码。当我运行此代码时,我得到:
selenium.common.exceptions.nosuchelementexception:消息:无此类元素:
找不到元素:{“方法”:“css选择器”,“选择器”:“[id=“phonenumberid”]”},
而且它不会输入电话号码。但是当我删除try-execute块时,它会输入电话号码,这很奇怪,因为try-execute块不应该影响上面的代码吗?另外,我放置的css选择器肯定存在,我可以在html中看到它。感谢您的帮助!以下是此部分的代码:

  1. phonenum = browser.find_element_by_id("phoneNumberId")
  2. phonenum.send_keys('8888888888')
  3. try:
  4. ll = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.FliLIb.DL0QTb')))
  5. except:
  6. print('error')
jckbn6z7

jckbn6z71#

您应该添加等待,让 phonenum 元素已加载。
这样地:

  1. from selenium.webdriver.common.by import By
  2. from selenium.webdriver.support.ui import WebDriverWait
  3. from selenium.webdriver.support import expected_conditions as EC
  4. wait = WebDriverWait(driver, 20)
  5. phonenum = wait.until(EC.visibility_of_element_located((By.ID, "phoneNumberId")))
  6. phonenum.send_keys('8888888888')

相关问题