简体   繁体   English

如果与搜索值匹配,如何点击搜索引擎结果

[英]How to click on a search engine result if it matches the searched value

I'm new to selenium.我是 selenium 的新手。 I'm taking user input and based on that, I'm searching it in duckduckgo.我正在接受用户输入并基于此,我正在duckduckgo 中搜索它。 I want, if the input value matches the search result weblink, the code should click on that matched website.我想,如果输入值与搜索结果 web 链接匹配,则代码应点击匹配的网站。 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

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.我输入了这个输入值 - Mitchell Centre, Darwin CBD, 0800我的代码运行并打开了duckduckgo并在搜索栏中输入了这个值,它应该点击与我的输入值匹配的网站(在蓝色气泡中),但是代码在此之后停止。

在此处输入图像描述

I don't understand what the problem is, would be great if you guys could help me.我不明白问题是什么,如果你们能帮助我,那就太好了。

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实际上我来自 Java 背景,但如果你可以将我的两行 java 代码转换为 python 你将能够实现它

Your code:你的代码:

for link in b: url = link.get_attribute('href') if any(dom in url for dom in list): link.click() break对于 b 中的链接:url = link.get_attribute('href') 如果有(url 中的 dom 列表中的 dom):link.click() 中断

Instead of link.get_attribute, Convert the above code as below而不是link.get_attribute,转换上面的代码如下

In Java we have.getText() method to get the Text.在 Java 我们有.getText() 方法来获取文本。

So try to get the text which your looking for using python as below(I used Java)因此,尝试使用 python 获取您要查找的文本,如下所示(我使用了 Java)

link.getText() store this in a variable

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

And then here comes your if Condition然后你的 if 条件来了

use your contains method as in python the below one is in Java使用您的包含方法,如 python 下面的一个在 Java

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

link.click()
break

This code is working fine for me.这段代码对我来说很好用。

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

相关问题 如何从谷歌 API(可编程搜索引擎)获得零点击搜索结果? - How to get Zero click search result from google API (Programmable Search Engine)? Python 字符串搜索:如何查找完全匹配,而不是与包含搜索字符串的字符串匹配 - Python string search: how to find exact matches, and not match with strings that contain searched string in them 如何从 es.search 结果中的列表中获取搜索到的字符串? - how can I get the searched string from list in my es.search result? 如何在https://www.airdna.co/上单击第一个自动建议或第一个搜索的值 - How to click on the first auto suggestion or first searched value on https://www.airdna.co/ 提取谷歌搜索引擎结果 - Extract Google Search Engine Result 如何在线搜索值并检查它是否符合给定条件 - How to search for a value in line and check if it matches given condition Unqlite python如何搜索符合要求的值的集合 - Unqlite python how to search collection for value that matches requirement 如何使用 Python Selenium 点击第一个 Yahoo 搜索结果 - How to click on the first Yahoo search result using Python Selenium 如何使用selenium-webdriver单击Google搜索结果 - How can i click Google search result with selenium-webdriver 如何使用python selenium点击第一个谷歌搜索结果? - How to click on the first google search result with python selenium?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM