【问题标题】:How to access image tag inside html inside iframe with python and selenium如何使用 python 和 selenium 访问 iframe 内的 html 内的图像标签
【发布时间】:2020-11-07 00:23:36
【问题描述】:

我正在尝试关闭弹出 iframe 以在 aliexpress 中进行网页抓取,但我无法访问图片以点击它

链接:https://www.aliexpress.com/item/4000094534783.html?spm=a2g0o.productlist.0.0.4e024c1e5Hpt1O&algo_pvid=501da7fc-b3f5-46a9-bbbc-75c7bed32f81&algo_expid=501da7fc-b3f5-46a9-bbbc-75c7bed32f81-20&btsid=0b0a555716045883196526344e54cc&ws_ab_test=searchweb0_0,searchweb201602_,searchweb201603_

Where i want to click

这是我的代码:

import os
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

option = Options()
option.set_headless(True)
profile = webdriver.FirefoxProfile()
profile.set_preference('intl.accept_languages', 'fr')

def webscraping_aliexpress():
    driver = webdriver.Firefox(executable_path=f"{os.getcwd()}\geckodriver.exe", firefox_profile=profile)
    driver.get(str(input("Paste aliexpress url: ")))
    name = driver.find_element_by_xpath("//h1[@class='product-title-text']").text
    price = driver.find_element_by_xpath("//div[@class='product-price-current']").text
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe[@data-spm-anchor-id='a2g0o.detail.0.i4.4bc257b6555T2w'")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[@class='rax-image ']"))).click()

webscraping_aliexpress()

我可以在弹出窗口显示之前提取一些数据,例如名称和价格,但之后他会阻止其他操作。

【问题讨论】:

  • 是你的问题,无法关闭弹窗或随机弹窗多个弹窗?
  • 只出现一个弹窗,但我无法关闭它

标签: python selenium web-scraping


【解决方案1】:

您需要将内容切换到 iframe

iframe = driver.find_element_by_xpath('//iframe[contains(@src, "__poplayer")]')
driver.switch_to.frame(iframe)
# code of closing popup here
driver.switch_to_default_content()

【讨论】:

    猜你喜欢
    • 2015-11-17
    • 2012-09-10
    • 2021-11-01
    • 1970-01-01
    • 2021-08-07
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多