首页 > 解决方案 > 跨 IFRAME 内的文本抓取

问题描述

[1]跨度我试图刮

使用进口:

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

我已经更改为默认值,然后更改为 orderInfo Iframe :

driver.switch_to.default_content()
def changeIframetoOrders():
    wait2 = WebDriverWait(driver, 10)
    wait2.until(EC.frame_to_be_available_and_switch_to_it((By.NAME, "orderInfo")))

我试过 XPath

driver.find_element_by_css_selector("//html/body/div[1]/div/div/div[4]/div/div/div[2]/table"
                                                    "/tbody/tr/td/div/span[1]").text)

我总是按照规则更改为默认 iframe,然后在其他 iframe 上工作,仍然有时会出现 TimeOut ERROR 或找不到元素。

我的整个代码 - 已更新

# when i get to this DEF im in moduleManagment iframe
DATELIST = list()
wait5 = WebDriverWait(driver, 10)
# that click working in the module iframe
driver.find_element_by_xpath(
    "//html/body/div[1]/div/div/div[3]/div[2]/div[1]/table/tbody/tr[" + str(rownumber) + "]/td[7]/div[1]/a").click()
# changing to orders iframe as i listed above
changeIframetoOrders()
# work on this
# print(driver.find_element_by_xpath("//div[contains(@class,'activitiy-log-msg')]/span").text)
# trying appending to a list the content of the span ...
DATELIST.append(driver.find_element_by_xpath("//div[contains(@class, 'activitiy-log-msg')]/span").text)
print(DATELIST)
# from here to the end im exit to the module iframe to click some Exit button.
driver.switch_to.default_content()
changeIframetoManagement()

wait5.until(
    EC.visibility_of_element_located(
        (By.CSS_SELECTOR, "div.modal-dialog button.bootbox-close-button"))).click()
return DATELIST

错误 :

Traceback (most recent call last):
File , 
line 136, in <module>
changeIframetoOrders()

line 41, in changeIframetoOrders
wait2.until(EC.frame_to_be_available_and_switch_to_it((By.NAME, 
"orderInfo")))
 
packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

标签: python-3.xseleniumweb-scrapingiframewebdriver

解决方案


你可以试试xpath

//div[contains(@class, 'activitiy-log-msg')]/span

代码 :

print(driver.find_element_by_xpath("//div[contains(@class, 'activity-log-msg')]/span").text)

更新 1:

# when i get to this DEF im in moduleManagment iframe
DATELIST = list()
wait5 = WebDriverWait(driver, 10)
# that click working in the module iframe
driver.find_element_by_xpath(
    "//html/body/div[1]/div/div/div[3]/div[2]/div[1]/table/tbody/tr[" + str(rownumber) + "]/td[7]/div[1]/a").click()
# changing to orders iframe as i listed above
driver.switch_to.default_content()
changeIframetoOrders()
# work on this
print(driver.find_element_by_xpath("//div[contains(@class, 'activitiy-log-msg')]/span").text)
# trying appending to a list the content of the span ...

推荐阅读