Home > Blockchain >  Click on link by Python
Click on link by Python

Time:03-06

I have a part of HTML, and I need Python/selenium script to click on elelement "Odhlásit se"

               <div >
                    <ul  data-responsive-menu="accordion medium-dropdown">
                        <li >
                            <a href="" style="padding: 19px 16px !important;">|</a>
                        </li>
                                                                                <li>
                                <a href="/profile">Můj profil</a>
                            </li>
                            <li>
                                <a href="/logout">Odhlásit se</a>
                            </li>
                                                                    </ul>
                </div>


           

But find_element_by_link_text and find_element(by="link text") not work.

from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path = "C:\selenium browser drivers\chromedriver.exe")
driver.get("https://cs.laurie-project.com/login") #načtení stránky
confirmation = driver.find_element(by="xpath", value="//button[@aria-label='Close reveal']")
confirmation.click() #potvrzení vyskakovacího okna
login_field = driver.find_element(by="id", value="username-label")
login_field.send_keys("TestovaciUcet")# Vyplnění uživatelského jména
password_field = driver.find_element(by="id", value="pw-label")
password_field.send_keys("Heslo123")#Vyplnění hesla
login_button = driver.find_element(by="id", value="register-label")
login_button.click()#Odeslání přihlášení
if driver.current_url == "https://cs.laurie-project.com/home":
        out_link = driver.find_element(by="link_text", value="logout")
        out_link.click()#Odhlášení
    else:
        print("Chyba přihlášení")#Chyba přihlášení

Any tips from somebody? Thanks.

CodePudding user response:

try:

out_link = driver.find_element_by_xpath("//a[text()='Odhlásit se']")

or:

out_link = driver.find_element_by_xpath('//a[contains(@href,"/logout.html")]')

I hope this works for you.

Thanks.

CodePudding user response:

The value of LINK_TEXT attribute is not logout but Odhlásit se. The href attribute contains the keyword logout. And better to use Explicit waits.

# Imports
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.get("https://cs.laurie-project.com/login")

wait = WebDriverWait(driver,30)

confirm = wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@aria-label='Close reveal']")))
confirm.click()

username = wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@id='username-label']")))
username.send_keys("TestovaciUcet")

password = wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@id='pw-label']")))
password.send_keys("Heslo123")

submit = wait.until(EC.element_to_be_clickable((By.ID,"register-label")))
submit.click()

if driver.current_url == "https://cs.laurie-project.com/home":
    # 1. Can use Link text - Odhlásit se to logout
    # logout = wait.until(EC.element_to_be_clickable((By.LINK_TEXT,"Odhlásit se")))
    
    # 2. Can use href attribute to click on logout button
    logout = wait.until(EC.element_to_be_clickable((By.XPATH,"//a[contains(@href,'logout')]")))
    logout.click()
else:
    print("Chyba přihlášení")#Chyba přihlášení
  • Related