相关疑难解决方法(0)

使用Python + Selenium选择iframe

所以,我对如何在Selenium中做到这一点感到困惑,并且无法在任何地方找到答案,所以我分享了我的经验.

我试图选择一个iframe并且没有运气(或者无论如何都没有重复).HTML看起来像这样:

<iframe id="upload_file_frame" width="100%" height="465px" frameborder="0" framemargin="0" name="upload_file_frame" src="/blah/import/">
<html>
    <body>
        <div class="import_devices">
            <div class="import_type">
                <a class="secondary_button" href="/blah/blah/?source=blah">
                    <div class="import_choice_image">
                        <img alt="blah" src="/public/images/blah/import/blah.png">
                    </div>
                    <div class="import_choice_text">Blah Blah</div>
                </a>
            </div>
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Python代码(使用selenium库)试图使用以下方法找到这个iframe:

    @timed(650)
def test_pedometer(self):
    sel = self.selenium
    ...
    time.sleep(10)
    for i in range(5):
        try:
            if sel.select_frame("css=#upload_file_frame"): break
        except: pass
        time.sleep(10)
    else: self.fail("Cannot find upload_file_frame, the iframe for the device upload image buttons")
Run Code Online (Sandbox Code Playgroud)

我找到的Selenium命令的每个组合都重复失败.偶尔的成功是不可复制的,所以也许是某种竞争条件或其他什么?从来没有找到正确的方法来获得适当的硒.

python selenium

30
推荐指数
4
解决办法
5万
查看次数

html中的Selenium和iframe

无法对iframe使用send_key().如何选择这个iframe以及其中的哪个元素应该用于send_key()?

页面图像

和iframe HTML代码

<iframe class="textarea" src="/framework/html/blank.html" style="width: 99%; border-width: 1px; height: 332px;">
#document
<html webdriver="true">
<head>
</head>
<body> … </body>
</html>
</iframe>
Run Code Online (Sandbox Code Playgroud)

如何将值发送到描述?

还有一件事我想知道当我在浏览器中查看"查看页面源"时,这个框架代码不会出现吗?

python iframe selenium selenium-webdriver

25
推荐指数
1
解决办法
3万
查看次数

13
推荐指数
2
解决办法
2万
查看次数

使用硒或要求填写表格

我试图进入此站点以检索我的银行帐户,首先我尝试使用硒,但仅填写了用户名(可能是因为它具有2种形式):

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://www.santandertotta.pt/pt_PT/Particulares.html")
user = driver.find_element_by_name("usr")
user.send_keys("user")
pas = driver.find_element_by_name("claveConsultiva")
pas.send_keys("password")
login = driver.find_element_by_id("login_button").click()
Run Code Online (Sandbox Code Playgroud)

然后,我进入了rambo模式:)试图弄清楚为什么我不能填充密码空间,以及使用请求的表单的隐藏值是什么,这是代码:

url = "https://www.particulares.santandertotta.pt/pagina/indice/0,,276_1_2,00.html"     
user_agent = {"user-agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/..."}
session = requests.session()
r = session.get(url)
soup = BeautifulSoup(r.text, "html.parser")    
data = {t['name']:t.get('value') for t in soup.find_all('input', attrs={'type': 'hidden'})}
print(data)    
Run Code Online (Sandbox Code Playgroud)

但是刚收到一个空洞的字典。通过登录和抓取进入网站的最佳方法是什么?

python selenium web-scraping python-3.x python-requests

7
推荐指数
2
解决办法
4316
查看次数

在执行一段时间后,Selenium 为所有网站提供“从渲染器接收消息超时”

我有一个应用程序,我需要一个长时间运行的Selenium Web 驱动程序实例(我在无头模式下使用Chrome 驱动程序 83.0.4103.39)。基本上,该应用程序不断从队列中提取 url-data,并将提取的 url 提供给 Selenium,Selenium 应该在网站上执行一些分析。许多这些网站可能已关闭、无法访问或损坏,因此我将页面加载超时设置为 10 秒,以避免 Selenium 永远等待页面加载。
我在这里遇到的问题是,经过一些执行时间(假设 10 分钟)Selenium 开始给出Timed out receiving message from renderer每个 url 的错误。最初它工作正常,它可以正确打开好的网站并在坏网站上超时(网站无法加载),但一段时间后它开始对所有内容超时,即使是应该正确打开的网站(我已经检查过,它们在 Chrome 浏览器上正确打开)。我很难调试这个问题,因为应用程序中的每个异常都被正确捕获。我也注意到这个问题只发生在headless模式中。

  • 更新 *
    在网站分析期间,我还需要考虑 iframe(仅顶级),因此我还添加了一个逻辑来将驱动程序上下文切换到主页中的每个 iframe 并提取相关的 html。

这是应用程序的简化版本:

import traceback
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

width = 1024
height = 768

chrome_options = Options()
chrome_options.page_load_strategy …
Run Code Online (Sandbox Code Playgroud)

python iframe selenium renderer selenium-chromedriver

7
推荐指数
1
解决办法
6065
查看次数

无论 html 元素在 selenium 中的哪个框架,我如何选择它?

我正在尝试选择一个位于 iframe 中的元素,并且可能位于其他 iframe 中。

是否有可能在(python)selenium 中的某个(子)iframe 中选择一个元素而不选择之前的 iframe?有没有办法以某种方式“循环”每个 iframe 并检查在哪里可以找到我的元素......?

以及如何在 case 元素和 html 内容以及 iframe 可能只是加载...的情况下做到这一点?

html python iframe selenium selenium-webdriver

6
推荐指数
1
解决办法
5285
查看次数

如何使用 Selenium 和 Python 抓取传单地图多边形

这篇文章与这篇文章非常相似:使用 selenium 和 python 在鼠标悬停后弹出时提取数据

但我无法找到我想要的答案。

我正在尝试抓取与此非常相似的传单地图:https://leafletjs.com/examples/choropleth/,理想情况下,我想下载将鼠标移到多边形上后出现的所有信息:

原始帖子循环遍历每个圆元素,我想对每个多边形执行相同的操作。

代码试验:

from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome
driver.get("https://leafletjs.com/examples/choropleth/")
timeout = 1000

explicit_wait30 = WebDriverWait(driver, 30)
try:
    # Wait for all circles to load
    poli = explicit_wait30.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.leaflet-interactive')))
except TimeoutException:
    driver.refresh()


data = []
i=1
for circle in poli:
    i+=1
    # Execute mouseover on the element
    driver.execute_script("const mouseoverEvent = new Event('mouseover');arguments[0].dispatchEvent(mouseoverEvent)", poli)
    # Wait for the data to appear …
Run Code Online (Sandbox Code Playgroud)

javascript python selenium leaflet selenium-webdriver

6
推荐指数
1
解决办法
456
查看次数

如何使用 Selenium 和 Python 单击#shadow-root(已关闭)内的按钮

我正在尝试单击#shadow-root (closed)iframe 内的按钮

<iframe title="recaptcha challenge expires in two minutes" 
   <div class="button-holder help-button-holder">
      #shadow-root (closed)
         <link rel="stylesheet" href="chrome-extension://mpbjkejclgfgadiemmefgebjfooflfhl/src/solve/solver-button.css">
         <button tabindex="0" title="Solve the challenge" id="solver-button"></button>
   </div>
</iframe>
Run Code Online (Sandbox Code Playgroud)

这就是我用来切换到 iframe 的方法

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@title='recaptcha challenge expires in two minutes']")))
Run Code Online (Sandbox Code Playgroud)

我怎样才能点击//button[@id="solver-button"]

这是它的真实外观的照片

iframe-链接

按钮链接

python selenium selenium-chromedriver selenium-webdriver shadow-dom

6
推荐指数
1
解决办法
4526
查看次数

如何在定位元素之前等待框架加载?

我正在尝试等待Selenium切换变化的帧,然后再等待另一个元素。即

var wait = new WebDriverWait(driver, 15);
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.Id("frameA"));

var wait2 = new WebDriverWait(driver, 15);
// wait for element within frameA to exist
wait2.Until(ExpectedConditions.ElementExists(By.Id("elementA")));
Run Code Online (Sandbox Code Playgroud)

如果我在Thread.Sleep(1000);第二次等待之前进行一次简单的操作,它的功能就很好,但是如果没有这样做,我会得到以下错误:

'unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot find context with specified id"}
    enter code here
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来等待框架上下文切换完成,然后再等待填充该框架中的元素?

c# selenium webdriver frames selenium-webdriver

5
推荐指数
1
解决办法
2538
查看次数

如何使用 Selenium 单击 cloudflare 的“验证您是人类”复选框挑战

我需要使用 Python 自动下载此网页中的 .csv 文件:

https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie
Run Code Online (Sandbox Code Playgroud)

现在,我写了这段代码:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
 
chromedriver_path = r"./driver/chromedriver"
browser = webdriver.Chrome(executable_path=chromedriver_path)
url = "https://pace.coe.int/en/aplist/committees/9/commission-des-questions-politiques-et-de-la-democratie"
topics_xpath = '//*[@id="challenge-stage"]/div/label/span[2]'
browser.get(url)
time.sleep(5)  #Wait a little for page to load.
escolhe = browser.find_element("xpath", topics_xpath)
time.sleep(5)
escolhe.click()
time.sleep(5)
Run Code Online (Sandbox Code Playgroud)

网页打开,然后提示我单击“验证您是人类”:

在此输入图像描述

我已经“检查”了按钮并复制了 xpath(参见上面的代码)。但我收到这个错误:

NoSuchElementException: no such element: Unable to …
Run Code Online (Sandbox Code Playgroud)

iframe xpath cloudflare selenium-webdriver webdriverwait

5
推荐指数
2
解决办法
9741
查看次数