简体   繁体   English

从字典列表返回值列表,其中键等于某个值

[英]Returning a list of values from a list of dictionaries where keys equal a certain value

<div id="tabs" class="clearfix">
    <ul id="remove">
        <li class="btn_arrow_tab left inactive">
            <a href="#" class="doubleText">Pay Monthly <small>View standard rates and Bolt Ons</small>
            </a>
        </li>
        <li class="btn_arrow_tab right inactive">
            <a href="#" class="doubleText">Pay &amp; Go<small>View standard rates and Bolt Ons</small>
            </a>
        </li>
    </ul>
</div>

I have no experience in webscraping and trying to follow example and the docs to click on the button with text 'Pay Monthly'. 我没有网络爬虫的经验,也没有尝试遵循示例和文档以单击带有“按月支付”文本的按钮的经验。 This button then dynamically displays some text which I need to copy. 然后,此按钮将动态显示一些我需要复制的文本。 How do I go about clicking this for starters, and then reading the text which is displayed. 我该如何单击入门指南,然后阅读显示的文本。 I am trying it with Selenium, would beautifulsoup be better? 我正在用Selenium尝试,beautifulsoup会更好吗? I have been trying this line of code but it isn't doing anything: 我一直在尝试这一行代码,但是它什么也没做:

driver.find_element_by_xpath("//a[text()[contains(.,'Pay Monthly')]]").click()

It is always good practice to use mixture of absolute and relative xpath to locate a element. 最好使用绝对xpath和相对xpath的混合来定位元素。

First thing you should find is a parent that has a unique identifier. 您应该找到的第一件事是具有唯一标识符的父母。 The element you mentioned has two parent items with a static id. 您提到的元素具有两个具有静态ID的父项。 One is root div and another is ul . 一个是root div ,另一个是ul

Now either we can follow your path and find the element using Text. 现在,我们可以按照您的路径使用Text查找元素。 Any of the following shall work. 以下任何一项均有效。

driver.find_element_by_xpath("//div[@id='tabs']//a[text()[contains(.,'Pay Monthly')]]").click()
driver.find_element_by_xpath("//ul[@id='remove']//a[text()[contains(.,'Pay Monthly')]]").click()

But, if the item is static element and considering your goal here, I would suggest the following method. 但是,如果该项目是静态元素并考虑您的目标,则建议使用以下方法。 indexing your xpath when it returns multiple elements. 当xpath返回多个元素时索引它。

myElement = driver.find_element_by_xpath("//div[@id='tabs']//a[@href='#'][1]")
myElement.click()

And then you can capture the text. 然后,您可以捕获文本。 You can put some wait to ensure the text gets changed. 您可以wait以确保文本被更改。

myText = myElement.text

Let me know if this doesn't work. 让我知道这是否无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从字典列表中获取某些键的值 - How to get values for certain keys from a list of dictionaries 从Python字典列表到CSV中选择某些键,值 - Selecting Certain Keys,Values From List of Python Dictionaries to CSV 如何对字典中的键进行排序,添加值并在组合值等于某个数字时返回键列表 - How to sort through keys in a dictionary, adding the values and returning a list of keys if combined values equal a certain number 如何检查字典列表中键的值是否都等于 0 - How check if values of keys in list of dictionaries are both equal 0 从键列表和值列表创建字典列表? - create a list of dictionaries from a list of keys and a list of list of values? 从列表中的字典中获取键和值 - Get keys and values from dictionaries in a list 如何从键列表和值列表创建字典列表 - How to create a list of dictionaries from a list of keys and a list of values 键列表和值列表中所有字典的列表 - List of all dictionaries from list of keys and list of values 从字典列表中返回具有最高值的键 - Returning the Key with the highest Value from a List of Dictionaries Pandas,字典列表,其中值是一个列表。 将字典键转换为列名。 将值列表中的每个元素转换为一行 - Pandas, list of dictionaries where values are a list. Convert dictionary keys into column names. Convert each element in value list to a row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM