使用python访问selenium web元素

时间:2011-11-14 12:53:43

标签: python selenium

我确信这已经在某个地方得到了解答,因为这是一个非常基本的问题 - 但是,对于我的生活,我不能在网上找到答案。我觉得自己像一个完全白痴,但我必须这样问,这里是:

我正在编写一个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变量不包含页面上找到的链接 - 而是列表包含(我在这里猜测)特定于selenium的对象,称为WebElements。但是,我不能找到任何能给我链接的WebElement属性 - 事实上我找不到任何在python中访问的WebElement属性的例子(至少不能以我能重现的方式)

我非常感谢你们能给我的任何帮助

此致 新秀

2 个答案:

答案 0 :(得分:18)

我熟悉python的seiium 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)

答案 1 :(得分:0)

  

我一直在检查你的提示,不要使用time.sleep(10)作为页面加载等待。通过阅读不同的帖子,我觉得等待页面加载对于selenium 2来说是多余的.Se例如链接原因是selenium 2有一个隐含的等待加载功能。我以为我会提到你,因为你花时间回答我的问题。

有时硒的行为方式不清楚。有时硒会引发对我们不感兴趣的错误。

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的初学者。当然,代码已经简化了。