A
A
anya_hacker2021-03-24 23:38:35
Python
anya_hacker, 2021-03-24 23:38:35

How to click on a link?

I wrote a program that logs in through a proxy, clears cookies, installs a fake user agent, and all this with the help of selenium. I write a request to Yandex: fix the scooter.
If there is a site prostomesto.ru in the Yandex search results, then you need to go to it.
But for some reason, selenium does not see it (the line driver.find_element_by_link_text(site_link).click() for some reason does not follow the link called prostomesto.ru)
How to check that the link is prostomesto.ru and actually follow it?
The code:

from seleniumwire import webdriver
from fake_useragent import UserAgent

from time import sleep


def yandex():
    login = 'R5edkrXE'
    password = 'rU1Ff396'
    proxy_adress_now = '194.59.12.83:56451'

    proxy_options = {
        "proxy": {
            "https": f"https://{login}:{password}@{proxy_adress_now}"
        },
        "User-Agent": UserAgent().chrome  # фейковый юзер-агент
    }

    driver = webdriver.Chrome(
        executable_path='chromedriver.exe',
        seleniumwire_options=proxy_options
    )
    driver.maximize_window()
    driver.delete_all_cookies()  # очищаю куки

    try:
        site_link = 'prostomesto.ru' # если написать "Самокаты есть у нас! Смотри прямо сейчас!", то он кликает на ссылку
        url = f"https://yandex.ru/search/direct?&text=починить самокат"
        driver.delete_all_cookies() # снова очищаем куки чтоб уже наверняка
        driver.get(url)
        print("открыли сайт яндекса")
        sleep(25)
        try:
            driver.find_element_by_link_text(site_link).click()  # открываем сайт, если его ссылка - prostomesto.ru
            sleep(3)
        except Exception:
            print(f"сайта {site_link} нет в рекламной выдаче яндекса")
            pass


    except Exception as e:
        print(e)

    finally:
        driver.close()
        driver.quit()


if __name__ == '__main__':
    yandex()


605bb12459c41882950817.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeniy _, 2021-03-25
@anya_hacker

You probably can't find it because prosto...ru is in the b tag .
find_element_by_link_text finds the element if it is under the a tag .
Search by xpath .

A
alekssamos, 2021-03-25
@alekssamos

My theory: if Yandex marked advertising links with some class, then blockers would only eat them that way, so the links need to be mixed with organic ones.
Well, or look for a matched URL address (href), some type selector
Right-click > view the element code
and compose the selector. In short, try either through find_element_by_css_selector, or through find_element_by_class_name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question