D
D
DigiBeach2020-07-08 22:15:50
Python
DigiBeach, 2020-07-08 22:15:50

How to click on links we get with element.text attribute in chromedriver?

Good afternoon. In general, in C # there is such a thing as checking the text of links that is displayed in tolist and clicking on it if the text matches the one that we set, or clicking on everything else except for the text that we set.
The question is how to do this in Python?

The code is like this

driver = webdriver.Chrome()
driver.get('https://yandex.ru/')
for element in driver.find_elements_by_css_selector('#news_panel_news'):
    print(element.text)


After that, we get the texts of all news links in the console. How to check the text and if the text of the link is necessary for us, then we do not click on it, and if this text is not there, then we open the link, we open the links one by one.

It turns out that I need some kind of sorting of links by numbers, for example. And then there is a check of each number. If link number 1 does not have our text, then click on it, if the second one does, then do not click, and so on. How to sort them and check and click?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
ScriptKiddo, 2020-07-08
@ScriptKiddo

if 'text' not in element.text:
    element.click()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question