【问题标题】:How to select this text field with Selenium webdriver?如何使用 Selenium webdriver 选择此文本字段?
【发布时间】:2016-05-11 21:32:30
【问题描述】:

如何为 selenium webdriver 测试选择信用卡号字段?

https://secure-store.nike.com/us/checkout/html/billing.jsp?_requestid=173323

cc = driver.find_element_by_id("creditCardNumber")

我在这一行遇到无法定位元素错误,我不知道为什么。

# click next button
driver.find_element_by_id("shippingSubmit").click()

# enter credit card number
cc = driver.find_element_by_id("creditCardNumber")
cc.click()
cc.clear()
cc.send_keys("4411111111111111")

# enter expiration
selectmonth = Select(driver.find_element_by_id("expirationMonth"))
selectmonth.select_by_value("10")

# enter expiration year
selectyear = Select(driver.find_element_by_id("expirationYear"))
selectyear.select_by_value("2012")

【问题讨论】:

  • 我们无法访问您的网址。能否提供一些示例代码?
  • 是的,我更新了它,由于某种原因它无法通过 id 找到 creditCardNumber..
  • 能否也粘贴元素的html?
  • 由于您在此之前单击“下一步”按钮,我敢打赌您没有等待足够长的时间让该元素出现并变为可点击。因此,不要使用 find_element_by_id,而是尝试 WebDriverWait(driver, 10).until( EC. element_to_be_clickable((By.ID, "creditCardNumber")).click()
  • 我在该行遇到超时异常,我认为它无法通过 ID 找到?

标签: python selenium


【解决方案1】:

支付表单包裹在 iframe 元素中。在定位元素之前切换到它:

wait = WebDriverWait(driver, 10)
driver.switch_to.frame("billingFormFrame") 

cc = wait.until(EC.element_to_be_clickable((By.ID, "creditCardNumber"))
cc.click()
# ...

【讨论】:

  • 我不知道它在 iframe 中,谢谢!但是, cc.click() 似乎给了我一个错误。
  • 如果我更改为 cc = driver.find_element_by_id('creditCarNumber'),然后单击工作。但是我该怎么做你正在尝试的事情,即等待元素可点击,然后点击没有错误?
  • @david 好的,但是如果你先做 cc = wait.until(EC.element_to_be_clickable((By.ID, "creditCardNumber")) 然后再做 cc.click() 会发生什么?
  • 嗨,亚历克,感谢您的帮助。 NameError“未定义全局名称选择”
  • @david 当然,添加导入语句:from selenium.webdriver.support.select import Select
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 2016-04-01
  • 2012-07-09
  • 2015-08-19
  • 2014-10-31
  • 1970-01-01
相关资源
最近更新 更多