【问题标题】:accessing selenium web elements with python使用 python 访问 selenium web 元素
【发布时间】:2011-12-28 15:27:06
【问题描述】:

我确信这已经在某个地方得到了解答,因为这是一个非常基本的问题 - 但是,我终其一生都无法在网络上找到答案。我觉得自己像个白痴,但我不得不问,这里是:

我正在编写一个 python 代码,它将生成一个域上所有页面地址的列表。这是使用 selenium 2 完成的 - 当我尝试访问 selenium 生成的所有链接的列表时会出现问题。

这是我目前所拥有的:

from selenium import webdriver
import time

HovedDomene = 'http://www.example.com'
Listlinker = []
Domenesider = []
Domenesider.append(HovedDomene)

driver = webdriver.Firefox()

for side in Domenesider:        

        driver.get(side)
        time.sleep(10)
        Listlinker = driver.find_elements_by_xpath("//a")

        for link in Listlinker: 

            if link in Domenesider:
              pass
            elif str(HovedDomene) in str(link):
              Domenesider.append(side)

print(Domenesider)
driver.close()

Listlinker 变量不包含在页面上找到的链接 - 而是列表包含(我在这里猜测)称为 WebElements 的硒特定对象。但是,我找不到任何可以为我提供链接的 WebElement 属性 - 事实上,我找不到任何在 python 中访问的 WebElement 属性示例(至少不能以我可以重现的方式)

非常感谢大家能给我的任何帮助

真诚的 菜鸟

【问题讨论】:

  • 我第一次找不到 selenium 文档,而今天,我遇到了同样的问题(必须返回我的日志才能找到该页面)。我猜其他人可能有同样的问题,所以我决定在这里发布link,为了我和其他阅读本文的人。
  • 行 Listlinker = driver.find_elements_by_xpath("//a") 将生成一个不可迭代的 webdriver 对象。接下来如何在代码中使用 for 对其进行迭代?

标签: python selenium


【解决方案1】:

我熟悉python的selenium api 但您可能可以使用get_attribute(attributename) 方法接收链接。所以应该是这样的:

linkstr = ""
for link in Listlinker: 
  linkstr = link.get_attribute("href")

  if linkstr in Domenesider:
    pass
  elif str(HovedDomene) in linkstr:
    Domenesider.append(side)

【讨论】:

  • 顺便说一句,您也可能对此有一些问题: driver.get(side) time.sleep(10) driver.find_elements_by_xpath("//a") 因为页面可能未加载,您将收到NoSuchElementException。
  • 用著名的 Clay Davis 的话来说:'shiiiiiiiiit' - 因为 - 是的 - 我刚刚发现了!非常感谢您的回复 CMykyt - 成功了。在一个 selenium 文档页面中找到它,令人难以置信的是,直到我用谷歌搜索我得到的列表对象之一,我才看到它。
  • 感谢您指出 time.sleep VMykyt 的问题 - 我已经看到 selenium 有它自己的等待命令 - 我会实现这个!
  • 我一直在检查您的提示,即不要使用 time.sleep(10) 作为页面加载等待。通过阅读不同的帖子,在我看来,等待页面加载对于 selenium 2 来说是多余的。例如 link 原因是 selenium 2 具有隐式等待加载功能。只是想我会向您提及,因为您花时间回答了我的问题。
  • 我没有告诉你不要使用 time.sleep(xx)。我告诉你要注意;)。是的,硒相当不错,等到页面加载完毕。但是我们也可能有一些 ajax 控件和一些 js 效果等等。嗯...我将继续回答部分,因为我想发布一些代码示例
【解决方案2】:

我一直在检查您的提示,即不要使用 time.sleep(10) 作为页面加载等待。从阅读不同的帖子在我看来,等待页面加载对于 selenium 2 来说是多余的。Se 例如链接 原因是 selenium 2 具有隐式等待加载功能。只是想我会向您提及,因为您花时间回答了我的问题。

有时硒的行为方式不清楚。有时 selenium 会抛出我们不感兴趣的错误。

By byCondition;
T result; // T is IWebElement
const int SELENIUMATTEMPTS = 5;
int timeout = 60 * 1000;
StopWatch watch = new StopWatch();

public T MatchElement<T>() where T : IWebElement
{
    try
    {
        try {
            this.result = this.find(WebDriver.Instance, this.byCondition);
        }
        catch (NoSuchElementException) { }

        while (this.watch.ElapsedMilliseconds < this.timeout && !this.ReturnCondMatched)
        {

            Thread.Sleep(100);
            try {
                this.result = this.find(WebDriver.Instance, this.byCondition);
            }
            catch (NoSuchElementException) { }
        }
    }
    catch (Exception ex)
    {
        if (this.IsKnownError(ex))
        {
            if (this.seleniumAttempts < SELENIUMATTEMPTS)
            {
                this.seleniumAttempts++;
                return MatchElement();
            }
        }
        else { log.Error(ex); }
    }
    return this.result;
    }

    public bool IsKnownError(Exception ex)
    {
    //if selenium find nothing it throw an exception. This is bad practice to my mind.
    bool res = (ex.GetType() == typeof(NoSuchElementException));

    //OpenQA.Selenium.StaleElementReferenceException: Element not found in the cache
    //issue appears when selenium interact with other plugins.
    //this is probably something connected with syncronization
    res = res || (ex.GetType() == (typeof(InvalidSelectorException) && ex.Message
        .Contains("Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE)" +
                "[nsIDOMXPathEvaluator.createNSResolver]"));

    //OpenQA.Selenium.StaleElementReferenceException: Element not found in the cache
    res = res || (ex.GetType() == typeof(StaleElementReferenceException) && 
        ex.Message.Contains("Element not found in the cache"));

    return res;
}

对不起 C#,但我是 Python 的初学者。代码当然被简化了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    相关资源
    最近更新 更多