A
A
AnanasMaks2021-11-06 21:51:01
Python
AnanasMaks, 2021-11-06 21:51:01

How can I make xpath search work for me?

from selenium import webdriver
import time

options = webdriver.ChromeOptions()
#открытие браузера во всю ширину экрана 
options.add_argument("--start-maximized")

driver = webdriver.Chrome(
    executable_path="", 
    options=options
)

try:
    driver.get("https://rick-i-morty.online")
    time.sleep(5)

    play_all = driver.find_element_by_id("menu-item-3").click()
    time.sleep(5)
    episode = driver.find_element_by_xpath('//a[contains(@href,"https://rick-i-morty.online/episodes/3sez-1seriya161")]').click()
    time.sleep(10)
    play = driver.find_element_by_xpath('//*[@id="vpaid-but"]').click()
    time.sleep(10)
except Exception as ex:
    print(ex)
finally:
    driver.close()
    driver.quit()

console

driver = webdriver.Chrome(

DevTools listening on ws://127.0.0.1:65171/devtools/browser/9f860c67-0f1...
[28284:4988:1106/214646.242:ERROR:chrome_browser_main_extra_parts_metrics.cc(230)] crbug. com/1216328: Checking Bluetooth availability started. Please report if there is no report that this
ends
. 28284:4988:1106/214646.243:ERROR:chrome_browser_main_extra_parts_metrics.cc(236)] crbug.com/1216328: Checking default browser status started.
[28284:4988:1106/214646.246:ERROR:chrome_browser_main_extra_parts_metrics.cc(240)] crbug.com/1216328: Checking default browser status ended.
[28284:23308:1106/214646.247:ERROR:device_event_log_impl.cc(214)] [21:46:46.247] Bluetooth: bluetooth_adapter_winrt.cc:1073 Getting Default Adapter failed.
c:\Users\maksg\Desktop\selenium\selenium_headless_mode.py:18: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
play_all = driver.find_element_by_id("menu-item-3").click()
c:\Users\maksg\Desktop\selenium\selenium_headless_mode.py:20: DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead
episode = driver.find_element_by_xpath('//a[contains(@href,"https://rick-i-morty.online/episodes/3sez-1seriya161 ")]').click()
c:\Users\maksg\Desktop\selenium\selenium_headless_mode.py:22: DeprecationWarning: find_element_by_* commands are deprecated Please use find_element() instead
play = driver.find_element_by_xpath('//*[@id="vpaid-but"]').click()
Message: no such element: Unable to locate element: {"method":" xpath","selector":"//*[@id="vpaid-but"]"}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-11-06
@phaggi

Perhaps the element is not loaded by the time it is searched.
Try adding a line after the driver definition, this will make selenium try to find and click on the link for 15 seconds (not wait, but try periodically). Also make sure that the element you are trying to click is currently visible in the selenium browser; maybe you need to scroll first.driver.implicitly_wait(15)

after
Также, если вы не собираетесь серьезно работать с selenium в продакшене, а делаете наколенный проектик для потыкать в кнопки на сайте, то можно попробовать обертки над selenium, сильно упрощающие его синтаксис (например, elementium). В нем, к примеру, у поиска есть метод wait, с ним удобнее.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question