如何获取Selenium + Python的属性值 [英] How to get attribute value Selenium + Python

查看:76
本文介绍了如何获取Selenium + Python的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python处理Selenium.我想获取属性['href']的值,并检查它是否与'#'不同(如果是的话)
因此,请点击链接(如果不是非循环链接):

I'm working with Selenium in Python. I would like to get the value of an attribute ['href'] and check that it is different from '#', if yes
so click on the link if not out of the loop:

if not driver.find_element_by_link_text('»'):
        break
            # Open next page
    if driver.find_element_by_xpath("//a[.='»' and not(@href='#')]"):
        link=WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, "»")))
        link.click()
    else:
        break 

这是网页的源代码:

<ul class="pagination">
<li class="disabled"><a href="#">«</a></li>
<li class="active"><a class="" rel="nofollow" href="https://www.likibu.com/fr/search/39tuzgbpnycdv7tkj102g?guests=2&amp;destination_id=4094&amp;page=1">1</a></li>
<li><a class="" rel="nofollow" href="https://www.likibu.com/fr/search/39tuzgbpnycdv7tkj102g?guests=2&amp;destination_id=4094&amp;page=37">37</a></li>
<li><a class="" rel="nofollow" href="https://www.likibu.com/fr/search/39tuzgbpnycdv7tkj102g?guests=2&amp;destination_id=4094&amp;page=2">»</a></li>

我的代码不起作用,并且出现此错误:

My code does not work and I get this error:

引发exception_class(消息,屏幕,堆栈跟踪) selenium.common.exceptions.NoSuchElementException

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException

任何帮助将不胜感激.

推荐答案

您可以尝试一下,

elems = driver.find_elements_by_xpath("//a[@href]")
e = []
for elem in elems:
    val = elem.get_attribute("href")

    if "#" in val:
        print("found # do nothing")
    else:
        print("click this link ",val)
        // click link
        e.append(elem)
// call click function
e[0].click()

这篇关于如何获取Selenium + Python的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆