Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中使用beautifulsoup查找字符串的第二个匹配项_Python_Selenium_Web Scraping_Beautifulsoup - Fatal编程技术网

如何在python中使用beautifulsoup查找字符串的第二个匹配项

如何在python中使用beautifulsoup查找字符串的第二个匹配项,python,selenium,web-scraping,beautifulsoup,Python,Selenium,Web Scraping,Beautifulsoup,我有至少两个相同的按钮出现,我想点击一个特定的一个,这将很可能是第二个按钮。我把这两个按钮放在同一个盒子里 如何单击$21页?您没有提供足够的信息让我给出完整的答案,但一般来说,您可以使用BeautifulSoupfind\u all方法在HTML中找到多个(或全部)出现的某些功能。例如 [61]中的:html='hi和 …:再见 在[62]中:soup=BeautifulSoup(html'html.parser') 在[63]中:soup.find_all('a')) 出[63]: [,

我有至少两个相同的按钮出现,我想点击一个特定的一个,这将很可能是第二个按钮。我把这两个按钮放在同一个盒子里


如何单击$21页?

您没有提供足够的信息让我给出完整的答案,但一般来说,您可以使用BeautifulSoup
find\u all
方法在HTML中找到多个(或全部)出现的某些功能。例如

[61]中的
:html='hi和
…:再见
在[62]中:soup=BeautifulSoup(html'html.parser')
在[63]中:soup.find_all('a'))
出[63]:
[,
]
查看BeautifulSoup了解更多信息。值得注意的是,您不仅需要按名称搜索标记,还可以提供任意函数,只要它返回给定标记的True或False。例如

[64]中的
:soup.find_all(lambda标记:tag.name=='a')
出[64]:
[,
]
给定一个标记,您可能会发现
.has\u attr
.get
方法非常有用:

[71]:soup.find_all(lambda tag:tag.name=='a'和tag.has_attr('href')和'st …:tag.get('href')中的“ack” Out[71]:[]
请注意,如果为
.get
提供了一个缺少的属性,它将返回
None

尝试将attribute=value css选择器与contains运算符一起使用

driver.find_element_by_css_selector("[href*='Page$21']").click()

你能详细说明一下吗?我只能看到一个锚定标签。正如你提到的,你有两个相同的按钮,另一个在哪里?
  driver.find_element_by_link_text("...").click()
driver.find_element_by_css_selector("[href*='Page$21']").click()