Luận Đặng Văn
Luận Đặng Văn

Reputation: 51

how to click on link text selenium python

i tried to click on link text but no work. Unable to locate element: {"method":"link text","selector":"Bán chạy"}

please help me. My code: driver.find_element_by_link_text('Bán chạy').click()

https://shopee.vn/search?keyword=iphone

strong text

enter image description here

Upvotes: 0

Views: 713

Answers (3)

Shivam Sharma
Shivam Sharma

Reputation: 1

A link text is only used to identify the hyperlinks on a web page. It can be determined with the help of an anchor tag <a>. In order to create the hyperlinks on a web page, you can use anchor tags followed by the link text.

Here this element is inside a <div> tag so you can't select this element using linktext locator you can use XPATH or CSS_SELECTOR like following:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver=webdriver.Chrome()
driver.get("https://shopee.vn/search?keyword=iphone")
driver.find_element(By.XPATH,"//div[contains(text(),'Bán chạy')]").click()

Upvotes: 0

Akzy
Akzy

Reputation: 1928

You can do it by below way

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[contains(text(), 'Bán chạy')]")))
element.click()

OR

wait = WebDriverWait(driver, 20)
    element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[normalize-space(),'Bán chạy']")))
    element.click()

Upvotes: 0

msurwillo
msurwillo

Reputation: 3

driver.find_elements_by_xpath("//*[contains(text(), 'Bán chạy'')]").click()

Upvotes: -1

Related Questions