[英]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 & 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.