如何使用 selenium 选择搜索建议?该网站阻止我点击提交,需要选择

编程入门 行业动态 更新时间:2024-10-27 17:11:35
本文介绍了如何使用 selenium 选择搜索建议?该网站阻止我点击提交,需要选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我试图让我自己更轻松地搜索临时公寓,但是包含这些公寓列表的网站需要我从他们的下拉列表中选择一个建议,然后才能点击提交.无论搜索框中的条目多么完整.

这里的最终希望是我可以转到搜索结果,然后从每个列表中提取联系信息.我能够使用 Beautiful Soup 和 Requests 从列表中提取我需要的数据,但我必须将该特定列表的 URL 粘贴到我的代码中.我没有走那么远.如果有人对如何绕过着陆页以访问相关列表有任何建议,请告诉我.

我尝试通过查看成功搜索后的书写方式将城镇名称和州名称拼接到地址栏中,但没有奏效.

该网站是

所以我盲目地试图选择一些东西.

这里的错误是:

AttributeError: 'FirefoxWebElement' 对象没有属性 'select_by_value'

我用 select 尝试了一些东西,但这不适用于我尝试的方式.

我很难过,我能找到的解决方案是针对其他网站(例如 Google 或 Amazon)的,如果这样我就说不通了.

有谁知道我怎样才能做到这一点?

这是从列表中获取信息的代码,我必须对其进行扩展以获取其他数据:

import bs4, requestsdef getMonteurAddress(MonteurUrl):res = requests.get(MonteurUrl)res.raise_for_status()汤 = bs4.BeautifulSoup(res.text, 'html.parser')elems = soup.select('section.c:nth-child(4) > div:nth-child(2) > div:nth-child(2) > dl:nth-child(1) > dd:nth-child(2)')返回 elems[0].text.strip()address = getMonteurAddress('https://mein-monteurzimmer.de/105742/monteurzimmer/deggendorf-monteurzimmer-deggendorf-pensionfelix%40googlemailcom')print('Naslov je ' + address) #print 调用看看是否得到了正确的数据

解决方案

正如您在输入后所看到的,有一个正在创建的 div 列表.现在您需要为这些 div 获取有效的定位器.要获取这些创建的 div 的定位器,您需要在调试暂停模式下检查元素(F12--> 源选项卡 --> F8).

试试下面的代码,在您输入时选择第一个匹配的地址.

sbox = driver.find_element_by_xpath("//input[@placeholder='Adresse, PLZ oder Ort eingeben']")sbox.send_keys(searchterm)addessXpath = "//div[contains(text(),'"+searchterm+"')]";driver.find_element_by_xpath(addessXpath).click()

注意:如果有多个匹配的地址,将选择第一个.

I'm trying to make searching for temporary apartments a bit easier on myself, but a website with listings for these apartments requires me to select a suggestion from their drop down list before I can click on submit. No matter how complete the entry in the search box might be.

The ultimate hope here is that I can get forward to the search results and then extract contact information from each listing. I was able to extract the data I need from a listing using Beautiful soup and Requests, but I had to paste in the URL for that specific listing into my code. I didn't get that far. If anyone has a suggestion on how to perhaps circumvent the landing page to get to the relevant listings, please let me know.

I tried just splicing the town name and the state name into the address bar by looking at how it's written after a successful search but that didn't work.

The site is Mein Monteurzimmer.

Here is my code:

from selenium import webdriver
from selenium.webdrivermon.keys import Keys
from selenium.webdriver.support.select import Select


driver = webdriver.Firefox()


webpage = r"https://mein-monteurzimmer.de"
print('Prosim vnesi zeljeno mesto') #Please enter the town to search
searchterm = input()

driver.get(webpage)

sbox = driver.find_element_by_xpath("/html/body/main/cpagearea/section/div[2]/div/section[1]/div/div[1]/section/form/div/input")
sbox.send_keys(searchterm)

ddown = driver.find_element_by_xpath("/html/body/main/cpagearea/section/div[2]/div/section[1]/div/div[1]/section/form/div")
ddown.select_by_value(1)

webdriver.wait(2)

#select = driver.find_element_by_xpath("/html/body/main/cpagearea/section/div[2]/div/section[1]/div/div[1]/section/form/div")

submit = driver.find_element_by_xpath("/html/body/main/cpagearea/section/div[2]/div/section[1]/div/div[1]/section/form/button")
submit.click

When I inspect the search box I can't find anything related to the suggestions until I enter a text. Then I can't click on the HTML code because that dismisses the suggestions. It's quite frustrating.

Here's a screenshot:

So I'm blindly trying to select something.

The error here is:

AttributeError: 'FirefoxWebElement' object has no attribute 'select_by_value'

I tried something with select, but that doesn't work with the way I tried this.

I am stumped and the solutions I could find were specific for other sites like Google or Amazon and I couldn't make sense if it.

Does anyone know how I could make this work?

Here's the code for getting information out of a listing, which I'll have to expand on to get the other data:

import bs4, requests

def getMonteurAddress(MonteurUrl):
    res = requests.get(MonteurUrl)
    res.raise_for_status()

    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    elems = soup.select('section.c:nth-child(4) > div:nth-child(2) > div:nth-child(2) > dl:nth-child(1) > dd:nth-child(2)')
    return elems[0].text.strip()




address = getMonteurAddress('https://mein-monteurzimmer.de/105742/monteurzimmer/deggendorf-monteurzimmer-deggendorf-pensionfelix%40googlemailcom')
print('Naslov je ' + address) #print call to see if it gets the right data

解决方案

As you can see once you type in, there is a list of divs creating. Now you need to get the a valid locator for these divs. To get the locator for these created divs you need to inspect elements in debug pause mode ( F12--> Source Tab --> F8).

Try below code to select first matching address as you typed.

sbox = driver.find_element_by_xpath("//input[@placeholder='Adresse, PLZ oder Ort eingeben']")
sbox.send_keys(searchterm)
addessXpath = "//div[contains(text(),'"+searchterm+"')]"
driver.find_element_by_xpath(addessXpath).click()

Note : If there are more than one matching address , first one will be selected.

这篇关于如何使用 selenium 选择搜索建议?该网站阻止我点击提交,需要选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 08:07:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1132398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   建议   网站   selenium

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!