简体   繁体   English

Selenium 不打开下拉按钮。 Html 元素名称正确,但有问题

[英]Pull-down buttons do not open with Selenium. Html element name correct, but something is wrong

After opening the Google News section, I would like to click on the Tools button, then All results and finally Past 24 hours .打开谷歌新闻部分后,我想点击Tools按钮,然后点击All results ,最后点击Past 24 hours I'd rather use the By.LINK_TEXT mode as a show in the code.我宁愿使用By.LINK_TEXT模式作为代码中的显示。

The News button works fine, everything ok .新闻按钮工作正常,一切正常 The problem is that the buttons are not clicked.问题是按钮没有被点击。 The problem does not concern By.LINK_TEXT, because I have also tried with CSS_SELECTOR and XPATH.这个问题与 By.LINK_TEXT 无关,因为我也尝试过使用 CSS_SELECTOR 和 XPATH。 There is something wrong.有问题。 Can you help me?你能帮助我吗? Below I attach the code and the screenshot to make you understand where I would like to click.下面我附上代码和屏幕截图,让您了解我想点击的地方。 Again, I would prefer a solution with By.LINK_TEXT同样,我更喜欢 By.LINK_TEXT 的解决方案

NOTE: The words to be searched for are previously entered via a variable that is automatically inserted in the Google search textbox, so as a solution I cannot enter the words to be searched for in the link注意:要搜索的单词之前是通过自动插入 Google 搜索文本框中的变量输入的,因此作为解决方案,我无法在链接中输入要搜索的单词

driver.get("https://www.google.com/")    

word = "example"

insert_word = driver.find_element(By.NAME, 'q')
insert_word.send_keys(word)  

insert_word.submit()

Code代码

news = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "News"))).click()

tools = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Tools"))).click()

recent = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "All results"))).click()

past24hours = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Past 24 hours"))).click()

在此处输入图像描述

hey max how about instead of entering the search query (word) manually, u feed it directly to the url, then u do the driver.get() and while your at it, since you know you want the news page and the past 24 hours, you can also feed these parameters to the url as well, so the code will look somthing like this:嘿,最大怎么样,而不是手动输入搜索查询(单词),你直接将它提供给 url,然后你执行driver.get()并在你的时候,因为你知道你想要新闻页面和过去 24小时,您也可以将这些参数提供给 url,所以代码看起来像这样:

from time import sleep
from seleniumwire import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
word = "example"
url = "https://www.google.com/search"
url += "?q=" + word # the search word
url += "&tbm=nws" # the news page
url += "&hl=en"  # language
url += "&tbs=qdr:d" # the past 24 hours
# url devided to dowmain and arguments
# tbs : qdr:h -> time range: last hour
# tbs : qdr:d -> time range: last day
# tbs : qdr:m -> time range: last mounth
# tbs : qdr:y -> time range: last year
# tbs : ar:1 -> time range: archive
# tbm : nws ->type: news
# tbm : blg ->type: blog
# tbm : vid ->type: video
# tbm : isch ->type: image
# q : query to search for (search term)
driver.get(url)

sleep(5)

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

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