【问题标题】:Python Selenium clicks links if contain some specific 'words'如果包含某些特定的“单词”,Python Selenium 会单击链接
【发布时间】:2021-01-21 17:23:16
【问题描述】:

我想点击所有包含某些特定单词的链接,链接(hrefs)如下:

什么是蓝色

如何将红色与黄色混合

粉红色的衣服好看吗?

它们都是我可以点击的链接,但我只想点击带有“蓝色”和“粉红色”字样的标题,否则,下一个链接,所以我这样做:但它只是说没有链接包含这个词

就像一个列表 = ['什么是蓝色','红色和黄色怎么搭配','粉色连衣裙好不好?'] 我想访问所有元素并检查 list = ['blue','pink'] 中的元素是否在其中

list = ['what is blue','how to mix red with yellow','is pink dress good?']
list2 = ['blue','pink'] 

如果list2中的任何元素也在list(hrefs)中,点击它,否则忽略它

提前感谢您的宝贵时间!

word = {'blue','pink'}
links = [link.get_attribute('href') for link in driver.find_elements_by_xpath('')] 
keywords= [keyword.get_attribute('innerHTML') for keyword in driver.find_elements_by_xpath('')] 
for line in keywords:
    if set(words).intersection(line.split(",")):
       print("in it")
       driver.get(link)

我也试过了,我想像标题一样包含蓝色的东西

keyword = driver.find_elements_by_xpath('//*[contains(@title, id)]')

html 看起来像

<a href="/zbscbzw/isinbm/202101/6b958a0fae1f497f8ec65b60d64120d9.shtml" target="_blank" title="new issue blue thing">New issue blue thing</a>

【问题讨论】:

  • 这能回答你的问题吗? Check if multiple strings exist in another string
  • 别想,因为 href 是一个带有 ',' 的列表,而不仅仅是 a_string = "A string is more than its parts!"
  • 嘿,打扰了,我想把这个添加到最后一个问题的解决方案中,find_elements_by_xpath('//*[contains(@title,keyword)]'): title contains 'keyword' list and also在日期 1-20 中,我该如何实现?

标签: python list selenium


【解决方案1】:

你可以让它比这更简单。基本上检查蓝色是否在每个单词的list1的字符串中。

list1 = ['what is blue','how to mix red with yellow','is pink dress good?']
list2 = ['blue','pink'] 
for l in list1:
    for x in list2:
        if x in l:
            print(x)
        else:
            print('No')

【讨论】:

  • 感谢您的帮助!但是当我打印 x 时,它给了我一个范围,而不是 list1 中的单词,对吗?
  • @Cathy 这是一个非常简单的检查。如果在元素中,这将打印蓝色或粉红色。
  • 我可以在stackoverflow和你聊天吗?
猜你喜欢
  • 2018-08-29
  • 2016-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
相关资源
最近更新 更多