2

I would like to click on the Google "News" button (after searching for something). I would like to search using the text News (in my case "Notizie") as an element. Google sometimes changes the names of the elements, so I would like to use the "News" element because it is always the same

I use this, but it doesn't work:

WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.CSS_SELECTOR, "div [aria-label = 'News']"))). Click ()

How can I do?

enter image description here

enter image description here

  • In the div tools, right click on that element and hover over copy and select copy by CSS selector, then use that in your scripts. – Hannon qaoud Sep 25 '22 at 22:57
  • @Hannon qaoud I know, but I wasn't looking for this way –  Sep 25 '22 at 23:00
  • Does this answer your question? [How do I find an element that contains specific text in Selenium WebDriver (Python)?](https://stackoverflow.com/questions/12323403/how-do-i-find-an-element-that-contains-specific-text-in-selenium-webdriver-pyth) – Hannon qaoud Sep 25 '22 at 23:03
  • @Hannon qaoud Google sometimes changes the names of the elements, so I would like to use the "News" element because it is always the same. Now check your links. Thank you –  Sep 25 '22 at 23:04
  • @Hannon qaoud No, I have not risen to my question –  Sep 25 '22 at 23:07
  • sorry, I am not sure I understand? so the link doesn't help? if so please explain one more time what is that you are trying to do. – Hannon qaoud Sep 25 '22 at 23:10
  • @Hannon qaoud No, the links not helped. The links come close to what I'm looking for, but they don't help. As you know, there are various ways to click on a button, I want to use a way that directly uses the name "News". I don't want to right click on an item and select something, but I want to use the name "News" directly. Also, I look for WebDriverWait (driver, 20) .until (EC.element_to_be_clickable, while in your links there was driver.find_element –  Sep 25 '22 at 23:21

1 Answers1

0

sorry for the delay.

after understanding you correctly, you want to search for an element by its text and click on it.

for the record, you use WebDriverWait (driver, 20) .until (EC.element_to_be_clickable while its a great practice you need to understand that if its your first click, then the driver will not click until all elements are visible, and therefore driver.find_element().click() does the same job

also a quick disclaimer, not all google searches show the "news" button in the options bar, and here's a sample code of what you want.

from time import sleep
from seleniumwire import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://www.google.com/search?q=weather')
newsButton = driver.find_element(By.LINK_TEXT, 'News')
newsButton.click()
sleep(5) 

you look for an element by its text with By.LINK_TEXT

and here's your code it it:

WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.LINK_TEXT, "News"))). Click ()

also check out By.PARTIAL_LINK_TEXT

good luck and sorry again for the misunderstanding, happy coding :)

update

it seems that the By.LINK_NAME filter only finds elements by text that are links, or in a <a> tag, and that's why it wont catch the "Tools" button, but the tools button is static, therefor you can access it with a normal css selector or xpath

Hannon qaoud
  • 785
  • 2
  • 21
  • Thank you. Using the same method, do you know why clicking on the News Tools button doesn't work? I wrote tools = WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.LINK_TEXT, "Tools"))). Click (), I don't understand why it doesn't work. –  Sep 25 '22 at 23:58
  • sorry for the inconvenience, the answer has been updated – Hannon qaoud Sep 26 '22 at 00:18
  • I'm trying to use tools = WebDriverWait (driver, 20) .until (EC.element_to_be_clickable ((By.XPATH, '// * [@ id = "hdtb-tls"]'))). Click (), but it doesn't work either . Maybe the problem is with the menu expansion function? –  Sep 26 '22 at 00:23
  • ```tools = driver.find_element(By.ID, "hdtb-tls")tools.click()``` try this – Hannon qaoud Sep 26 '22 at 00:25
  • It does not work. The Tools button is selected and colored with a gray rectangle, but it is not clicked –  Sep 26 '22 at 00:29
  • are you sure of the expected behavior? try to click it manually and see what happens, then report back if it doesn't do the same with the script. – Hannon qaoud Sep 26 '22 at 00:33
  • If I manually click on Tools, it works. If I click manually, the Google News menu opens (like a horizontal drop-down menu). Using the script, it doesn't open, it just colors Tools with a gray rectangle –  Sep 26 '22 at 00:40
  • 1
    If you want to help me, I posted a new question here https://stackoverflow.com/questions/73848713/pull-down-buttons-do-not-open-with-selenium-html-element-name-correct-but-some I'll be happy to accept this other answer as well if you help me –  Sep 26 '22 at 00:43