简体   繁体   English

Python Selenium 如果包含某些特定的“单词”,则单击链接

[英]Python Selenium clicks links if contain some specific 'words'

I'd like to clicks all links which contain some specific words, links (hrefs) are like:我想点击所有包含某些特定单词的链接,链接(hrefs)就像:

what is blue什么是蓝色

how to mix red with yellow如何混合红色和黄色

is pink dress good?粉色连衣裙好看吗?

They are all links which I can click, but I only want to click titles with word 'blue' and 'pink', else, next link, so I do: but it just says no links contain the word它们都是我可以点击的链接,但我只想点击带有“蓝色”和“粉红色”字样的标题,否则,下一个链接,所以我这样做:但它只是说没有链接包含这个词

it is like a list = ['what is blue','how to mix red with yellow','is pink dress good?'] and I want to access all elements and check if element in list = ['blue','pink'] is it in it它就像一个列表 = ['什么是蓝色','如何混合红色和黄色','粉红色的衣服好吗?'] 我想访问所有元素并检查列表中的元素 = ['蓝色','粉红色']里面有吗

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

if any elements in list2 is also in list(hrefs), click it, else ignore it如果 list2 中的任何元素也在 list(hrefs) 中,请单击它,否则忽略它

Thank you for your time in advance!提前感谢您的时间!

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)

also I tried, I want like title contains sth like blue我也试过了,我想像标题一样包含蓝色

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

the html looks like html 看起来像

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

You can probably make it simpler than this.你可以让它比这更简单。 Basically check if blue is in the string of list1 for each word.基本上检查 blue 是否在每个单词的 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')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM