使用Python发送密钥在Selenium中不起作用?

时间:2018-02-13 19:40:49

标签: python selenium selenium-webdriver sendkeys

我正在尝试将密钥发送到元素,但它无法正常工作。

Element looks like this. Unable to send keys to all three.

以下是代码:

  

输入id =" creditCardNumber"命名=" creditCardNumber"类型="电话"   的tabindex =" 1"数据短名称=" CC" MAXLENGTH =" 24"类=" CC-输入   ncss-input pt2-sm prl4-sm prl5-md pb2-sm"占位符="数"   onautocomplete ="关闭"值=""

我用它来发送密钥:

driver.find_element_by_xpath('//*[@id="creditCardNumber"]').send_keys('0000')

driver.find_element_by_xpath('//input[@placeholder="Number"]').send_keys('0000')

给我一个错误,好像元素不可用,虽然我使用了足够长的time.sleep

收到错误:

  

driver.find_element_by_xpath(' //输入[@placeholder ="数"]')send_keys。(' 0000')   文件   " /Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py" ;,   第293行,在find_element_by_xpath中       return self.find_element(by = By.XPATH,value = xpath)File" /Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py",   第752行,在find_element中       ' value':value})[' value'] File" /Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py" ,   第236行,执行中       self.error_handler.check_response(response)File" /Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py",   第192行,在check_response中       提出exception_class(message,screen,stacktrace)selenium.common.exceptions.NoSuchElementException:消息:没有这样的   element:无法定位元素:   {"方法":"的xpath""选择器":" //输入[@placeholder ="数"&# 34;}
  (会话信息:chrome = 64.0.3282.140)(驱动程序信息:   chromedriver = 2.35.528157   (4429ca2590d6988c0745c24c8858745aaaec01ef),平台= Mac OS X 10.12.2   x86_64的)

1 个答案:

答案 0 :(得分:2)

我注册了nike以查找creditCardNumber字段,它位于iframe标记下。切换到该iframe,然后搜索元素

<iframe sandbox="allow-scripts allow-same-origin" class="credit-card-iframe mt1 u-full-width" src="https://paymentcc.nike.com/services?id=0971b459-934f-4c0e-bdd1-6b5808a77eb1" frameborder="0" scrolling="no"></iframe>

代码:

driver.switch_to.frame(driver.find_element_by_css_selector('iframe[class="credit-card-iframe mt1 u-full-width"]'))
driver.find_element_by_id('creditCardNumber').send_keys('0000')

让我知道这是否有效