如果搜索引擎结果与搜索值匹配,如何单击它 [英] How to click on a search engine result if it matches the searched value

查看:141
本文介绍了如果搜索引擎结果与搜索值匹配,如何单击它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触硒.我正在接受用户输入,并以此为基础,在duckduckgo中进行搜索.我想要,如果输入值与搜索结果Web链接匹配,则代码应在该匹配的网站上单击.我的代码成功执行,但没有单击Web链接.这是我的代码:-

I'm new to selenium. I'm taking user input and based on that, I'm searching it in duckduckgo. I want, if the input value matches the search result weblink, the code should click on that matched website. My code executes successfully but it doesn't click on the weblink. This is my code:-

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException

stuff = input()
options = webdriver.ChromeOptions()
options.headless = True
options.add_argument("==lang=es")
browser = webdriver.Chrome()
browser.implicitly_wait(30)
browser.maximize_window()

browser.get('http://www.duckduckgo.com')
elem = browser.find_element_by_name('q')
elem.clear()

elem.send_keys(stuff)
elem.submit()

list = stuff.lower().replace(' ', '').split(',')
for a in browser.find_elements_by_id('links'):
    b = a.find_elements_by_tag_name('a')
    for link in b:
        url = link.get_attribute('href')
        if any(dom in url for dom in list):
            link.click()
        break

例如;我输入此输入值-Mitchell Centre, Darwin CBD, 0800,然后我的代码运行并打开duckduckgo并在搜索栏中输入此值,它应单击与我的输入值相匹配的网站(在蓝色气泡中),但此后代码停止.

For eg; I enter this input value - Mitchell Centre, Darwin CBD, 0800 and my code runs and opens duckduckgo and enters this value in the search bar, it should click on the website which matches my input value (in the blue bubble), but the code stops after this.

我不明白问题出在哪里,如果你们能帮助我,那会很好.

I don't understand what the problem is, would be great if you guys could help me.

推荐答案

实际上我来自Java背景,但是即使您可以将我的两行Java代码转换为python,也可以实现

Actually I am from Java background but still if you could convert my two lines of java code into python you would be able achieve it

您的代码:

对于b中的链接: url = link.get_attribute('href') 如果有的话(网址中的dom表示列表中的dom): link.click() 打破

for link in b: url = link.get_attribute('href') if any(dom in url for dom in list): link.click() break

而不是link.get_attribute,如下转换上面的代码

Instead of link.get_attribute, Convert the above code as below

在Java中,我们有.getText()方法来获取文本.

In Java we have .getText() method to get the Text.

因此,尝试使用python来获取您想要的文本,如下所示(我使用Java)

So try to get the text which your looking for using python as below(I used Java)

link.getText() store this in a variable

For ex as in Java : String str = link.getText()

然后是条件是否出现

使用python中的contains方法,下面的方法在Java中

use your contains method as in python the below one is in Java

if(str.contains(米切尔中心,达尔文中央商业区,0800"))

if(str.contains("Mitchell Centre, Darwin CBD, 0800"))

link.click()
break

此代码对我来说很好用.

This code is working fine for me.

这篇关于如果搜索引擎结果与搜索值匹配,如何单击它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆