Python:无法定位元素:s

2024-10-02 02:43:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着打开一个浏览器,用python脚本点击一个按钮 我的代码:

from selenium import webdriver
browser = webdriver.Chrome('/usr/local/bin/chromedriver')
browser.get('xxx')
browser.implicitly_wait(5)

button = browser.find_element_by_css_selector('#softGateBox > div.button_softgate > a')
button.click()

网站打开。它等待5秒钟,然后我看到错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#softGateBox > div.button_softgate > a"}

有什么问题吗?我使用chrome来检查按钮,并执行右键单击和单击复制选择器。你知道吗


Tags: 代码fromdivbrowser脚本selenium浏览器button
1条回答
网友
1楼 · 发布于 2024-10-02 02:43:19

这个页面没有什么问题

  • 元素位于<iframe>内,因此必须先找到<iframe>switch_to_frame(),然后才能搜索元素
  • <iframe>在外部<iframe>中,因此在开始搜索内部<iframe>之前,首先必须找到外部<iframe>switch_to_frame()
  • 在小型监视器上,元素是不可见的,所以硒可以点击它。你必须滚动页面到元素,然后你可以点击它。你知道吗

是的。你知道吗

from selenium import webdriver

browser = webdriver.Chrome() #'/usr/local/bin/chromedriver')

browser.get('https://www.facebook.com/SparColruytGroup/app/300001396778554?app_data=DD722A43-C774-FC01-8823-8016BFF8F0D0')
browser.implicitly_wait(5)

iframe = browser.find_element_by_css_selector('#pagelet_app_runner iframe')
browser.switch_to_frame(iframe)

iframe = browser.find_element_by_css_selector('#qualifio_insert_place iframe')
browser.switch_to_frame(iframe)

button = browser.find_element_by_css_selector('#softGateBox > div.button_softgate > a')
browser.execute_script("arguments[0].scrollIntoView(true);", button)

button.click()

顺便说一句:

页面上可以有其他<iframe>,所以您不能直接执行selector('iframe')。你知道吗

内部框架有id,但每次加载页面时它都会更改,所以不能执行selector('iframe#some_uniqe_id')

相关问题 更多 >

    热门问题