Home > Back-end >  How can i select this button and click it?
How can i select this button and click it?

Time:09-17

how can i select this button from list element using selenium and click it? this the html

<li class="ml1 sel">
    <a href="#__about.htm" id="about_page" onclick="return menuClick(this);" class="T sel">
        <span>about us</span>
    </a>
</li>

I have tried to use xpath css selector id and class name but always get error selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:

Is there anyone who can help me Please!

CodePudding user response:

you might be able to use the find_element_by_partial_link_text or find_element_by_link_text functions.

Try using this link for an idea on how to use it. Link

CodePudding user response:

There are 4 ways to click in Selenium.

I will use this xpath

//span[contains(text(), 'about us')]//parent::a[@id='about_page']

Code trial 1 :

time.sleep(5)
driver.find_element_by_xpath("//span[contains(text(), 'about us')]//parent::a[@id='about_page']").click()

Code trial 2 :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'about us')]//parent::a[@id='about_page']"))).click()

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(), 'about us')]//parent::a[@id='about_page']")
driver.execute_script("arguments[0].click();", button)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//span[contains(text(), 'about us')]//parent::a[@id='about_page']")
ActionChains(driver).move_to_element(button).click().perform()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
  • Related