【问题标题】:How to find an element in an <iframe> (none: id; none: classname) to action?如何在 <iframe> (none: id; none: classname) 中找到要操作的元素?
【发布时间】:2021-10-27 23:55:06
【问题描述】:

我正在寻找能够使用 selenium python 自动按住按钮的元素 xpath ,但是我遇到了困难,因为它们在 iframe 中。 我尝试了“Expected_conditions as EC”,但我的知识不足以让它们发挥作用。告诉我要改变什么,或更明智的方法。

HTML:

<div id="px-captcha" role="main">
  <iframe style="display: none; width: 310px; height: 100px; border: 0px; user-select: none;" token="951d7e81fd6fb5e2af2cb2c701dbb6c391ab81d4b983da5f2f2de85667241a43a3a814a87cae2e98c70b730f7eaaac0a04bbf77bbfc63735e436d1d07675cb68"></iframe>
  <iframe style="display: block; width: 310px; height: 100px; border: 0; -moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; -ms-user-select: none; user-select: none;" token="951d7e81fd6fb5e2af2cb2c701dbb6c391ab81d4b983da5f2f2de85667241a43a3a814a87cae2e98c70b730f7eaaac0a04bbf77bbfc63735e436d1d07675cb68">
    #document
    <html lang="en-US"
    <head>...</head>
    <body>
      <div id="kkBSsePnKDMVkwa" class="eIlUWbNLSMdFkEz">
        <div id="#LrJbZYBfdAzlAkl"></div>
        <div id="BlXIkuwFPcwvDCY" role="main" aria-label="Please press and hold the button until verified">...</div>
      </div>
    </body>
    </html>
  </iframe>
  <iframe style="display: none; width: 310px; height: 100px; border: 0px; user-select: none;" token="951d7e81fd6fb5e2af2cb2c701dbb6c391ab81d4b983da5f2f2de85667241a43a3a814a87cae2e98c70b730f7eaaac0a04bbf77bbfc63735e436d1d07675cb68"></iframe>
  <p style="color: red; margin-top: 4;">Please try again</p>
</div>

代码(更新)#错误:

def captcha(url):
    driver.get(str(url))
    time.sleep(10)
    try:
        captcha_element = driver.find_element_by_id('px-captcha')
        print(len(captcha_element.text), 'Captcha verification request page')
        print('Run pass captcha programing')
        # 2.1: Verify captcha
        # Research iframe containing captcha
        
        # # Example 2: Use pyautogui library
        # driver.set_window_position(0, 0)
        # driver.set_window_size(1024, 640)
        # sleep(randint(5,10))
        # pyautogui.moveTo(400, 438)
        # pyautogui.click()
        # pyautogui.dragTo(596, 438, 5, button='left')

        # Example 3:
        for i in range(10):
            try:
                wait = WebDriverWait(driver, 10)
                wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "(//div[@id='px-captcha']/iframe)[{i}]"))) #ERROR HERE. TO TRY REPLACEMENT <wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "(//div[@id='px-captcha']/iframe)[2]")))>
                print ("- Found iframe")
                element = driver.find_element(By.XPATH, "//div[contains(@aria-label, 'Please press and hold the button until verified')]")
                print ("- Found element")
                # click and hold 5 seconds to pass the captcha
                print("Button verify: ", len(element.text))
                action = ActionChains(driver)
                click = ActionChains(driver)
                frame_x = element.location['x']
                frame_y = element.location['y']
                print("x: ", frame_x)
                print("y: ", frame_y)
                print("size box: ", element.size)
                print("x max click: ", frame_x + element.size['width'])
                print("y max click: ", frame_y + element.size['height'])
                x_move = frame_x + element.size['width']/2
                y_move = frame_y + element.size['height']/2
                print("Click (x,y) = ", x_move, y_move)
                action.move_to_element_with_offset(element, x_move, y_move).click_and_hold().perform()
                time.sleep(10)
                action.release(element)
                action.perform()
                time.sleep(0.2)
                action.release(element)
                print('Verify successful')
                break
            except:
                print(f'- NOT Found xpath Num.: {i}')  
        sleep(randint(5,10))

    except:
        # 2.2: Skip captcha
        print('Website does NOT require captcha verification')
        sleep(randint(2,3))

我想要找到元素:

&lt;div id="BlXIkuwFPcwvDCY" role="main" aria-label="Please press and hold the button until verified"&gt;...&lt;/div&gt;

【问题讨论】:

  • 通过标签名获取iframes的数量列表。喜欢driver.findElements(By.tagName("iframe")); 并使用特定的iframe 进行切换。请告诉我结果。
  • TypeError: 'str' object is not callable

标签: python python-3.x selenium iframe captcha


【解决方案1】:

为了与这个网络元素交互:

<div id="BlXIkuwFPcwvDCY" role="main" aria-label="Please press and hold the button until verified">...</div>

你需要先切换到这个 iframe :

<iframe style="display: block; width: 310px; height: 100px; border: 0; -moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; -ms-user-select: none; user-select: none;" token="951d7e81fd6fb5e2af2cb2c701dbb6c391ab81d4b983da5f2f2de85667241a43a3a814a87cae2e98c70b730f7eaaac0a04bbf77bbfc63735e436d1d07675cb68">

既然你提到我们无法找到任何唯一标识符,我可能会使用它的父 div &lt;div id="px-captcha" role="main"&gt;

类似这样的:-

wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "(//div[@id='px-captcha']/iframe)[2]")))

然后可以与所需的网络元素进行交互。

driver.find_element(By.XPATH, "//div[contains(@aria-label, 'Please press and hold the button until verified')]").click()

您还需要以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

【讨论】:

  • 回溯(最近一次调用最后一次):第 185 行 wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//div[@id='px-captcha']/iframe [2]"))) 错误:“C:\Users\PC\AppData\Roaming\Python\Python39\site-packages\selenium\webdriver\support\wait.py”,第 80 行,直到引发 TimeoutException(消息,屏幕,堆栈跟踪)selenium.common.exceptions.TimeoutException:消息:
  • 我的命令行被上面的命令停止了。此外,我的 html 有 10 个相似的 iframe,并且在每个页面加载后,元素在这 10 个 iframe 中随机出现
  • 在 Chrome 中按 F12 -> 转到元素部分 -> 执行 CTRL + F -> 然后粘贴并查看//div[@id='px-captcha']/iframe[2],如果您想要的元素被突出显示
  • 试试这个(//div[@id='px-captcha']/iframe)[2]
  • @NandanA :我认为应该这样做,谢谢,我应该首先应用 xpath 索引。我已经在上面更新了,OP 可以尝试一下,让我们知道它是否有效。
猜你喜欢
  • 2022-01-20
  • 2019-11-27
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 2013-01-06
  • 2021-05-06
  • 1970-01-01
相关资源
最近更新 更多