python-3.x - 跨 IFRAME 内的文本抓取
问题描述
使用进口:
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:
解决方案
你可以试试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 ...
推荐阅读
- javascript - ACE 编辑器:如何将更改的数据移动到 $_POST 或 $_GET
- c - 是否要求指向两个不同本地空对象的指针比较不相等?
- google-cloud-ml - Google 的 mlengine 环境是否可以作为图像下载?
- styles - 如何启用 Sharepoint 2013 富文本编辑器功能区表格样式?
- android - 使用外键和房间的墓碑
- javascript - 监听 Web 组件上的全局事件
- rxjs6 - let 存在于 rxjs6 中吗?
- php - SQL 查询未插入
- amazon-web-services - 通过云形成控制 SNS 策略
- linux - 如何复制文件名与文本文件中的列表字符串部分匹配的文件?