使用 Selenium 和 Python 和 Safari 查找 CVV

Posted

技术标签:

【中文标题】使用 Selenium 和 Python 和 Safari 查找 CVV【英文标题】:finding CVV using Selemium and Python with Safari 【发布时间】:2021-07-05 09:09:55 【问题描述】:

我在一个零售商网站上找不到 CVV 输入字段。 这是我尝试过的。

searchXml = '//*[@id="payment-preferences"]/div[1]/fieldset/div[1]/div[2]/div[2]/input'
purchase = driver.find_element_by_xpath(searchXml)

这是来自 xpath 搜索的错误消息。我还添加了几个 time.sleep() 来减慢脚本的速度。

Traceback (most recent call last):
  File "Selenium_test.py", line 106, in <module>
    purchase = driver.find_element_by_xpath(searchXml)
  File "/Users/claybogusky/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Users/claybogusky/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, 
  File "/Users/claybogusky/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/claybogusky/Library/Python/3.8/lib/python/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: "method":"xpath","selector":"//*[@id="payment-preferences"]/div[1]/fieldset/div[1]/div[2]/div[2]/input"
  (Session info: chrome=89.0.4389.114)

谢谢!!

来自页面的 DOM 它显示一个 iFrame。

https://i.stack.imgur.com/nnAtY.jpg

【问题讨论】:

为您的问题添加了代码标签。以后请自己做。 回溯显示您的变量purchase 中有一个Web 元素实例,因此您的purchase = driver.find... 之一返回了匹配项。回溯还显示错误在purchase.find_element_by_name('cvv') 中,而不是在purchase = driver.find... 调用中。 您需要发布更多的 htmlinput 元素没有子元素,那么您为什么要尝试使用 purchase.find_element_by_name('cvv') 来查找带有 name="cvv"purchase 元素的后代? 是的,我处理了扭曲的输出。 “我处理了扭曲的输出。” -- 然后编辑您的问题并发布正确的文本。 【参考方案1】:

当您询问密码字段及其上方的搜索字段时,我对您的问题感到困惑。如果您只需要输入密码,您可以尝试:

purchase = driver.find_element_by_xpath("//input[@type='password']");

【讨论】:

感谢您的回复!该字段实际上是一个 CVV 输入字段,但 XML 路径和 HTML 表明输入类型是“密码”。我在上面尝试了您的代码,但遇到了同样的错误。不知何故,这个 CVV 输入字段隐藏得很好! 文件 "Selenium_test.py",第 101 行,在 中购买 = driver.find_element_by_xpath("//input[@type='password']") 文件 "/Users/claybogusky/Library /Python/3.8/lib/python/site-packages/selenium/webdriver/remote/webdriver.py",第 394 行,在 find_element_by_xpath 中找不到元素:"method":"xpath","selector":"//输入[@type='password']" 我认为我们需要在您的原始帖子中看到更多的 HTML/DOM。此按钮可能位于 iFrame 或您需要先切换到的类似内容中。 我在上面的帖子中添加了一个屏幕截图。再次感谢。 上图顶部有一个iframe!! 【参考方案2】:

CVV 位于 iFrame 中,因此我需要“进入”iframe,搜索并发送 CVV,然后返回父 iFrame 以完成购买。 我的 CC 存储在网站上我的个人资料下,因此我无需输入该信息。

这是代码。

输入CVV

time.sleep(20)

iframe = driver.find_element_by_xpath( '//*[@id="sppIframe"]')
driver.switch_to.frame(iframe)
searchXml = '//*[@id="payment-preferences"]/div[1]/fieldset/div[1]/div[2]/div[2]/input'
purchase = driver.find_element_by_xpath(searchXml)          
purchase.send_keys( v.cvv )
driver.switch_to.parent_frame()

time.sleep(10)
button_submit = '//*[@id="place-order-button"]'
placeOrderButton = driver.find_element_by_xpath(button_submit)   

【讨论】:

以上是关于使用 Selenium 和 Python 和 Safari 查找 CVV的主要内容,如果未能解决你的问题,请参考以下文章

使用 Selenium 和 Python,如何检查按钮是不是仍然可点击?

使用 Selenium 和 python 为 Instagram 提供上传文件路径

无法使用 Selenium 和 Python 访问下拉菜单

Selenium 窗口滚动仅适用于使用 Selenium 和 Python 识别 Select 元素的调试模式

Python Selenium 无法使用 Selenium 和 Python 在#shadow-root (open) 中通过 xpath 找到元素

如何使用 Python 和 Selenium 进行分页抓取页面