LearningNinja
LearningNinja

Reputation: 447

Can't click on an element using webdriver in python

I need help with click on an element using selenium webdriver in python.

Here is the html code for reference: I am trying to click on an element named "SYSTEM"

<body>
<div id="toparea">
<div id="logo" style="top: 10px; z-index: 3">
<div id="welcome">
<div id="topmenu1">
<ul>
<li id="home" class="selected" style="right: 636px">
<li id="incidents" style="right: 530px">
<li id="case" style="right: 424px">
<li id="capture" style="right: 318px">
<li id="policies" style="right: 212px">
<li id="classify" style="right: 106px">
<li id="system" style="right: 0px">
<a onclick="location.href='/imanager/ReShowMenuBar.do?selectedtab=system'" title="SYSTEM" target="mainFrame" href="/devicemgr/ReSystemAdministration.do">SYSTEM</a>
</li>
</ul>

I tried using:

driver.find_element_by_xpath("//*[@id='system']/a")

tried finding the element using link_text, id but all in vain.

I am also not sure if my xpath is correct, I am trying to learn.

Upvotes: 2

Views: 4818

Answers (3)

aleja20.06
aleja20.06

Reputation: 11

Please try:

driver = webdriver.Firefox();

driver.findElement(By.id("SYSTEM")).click();

Upvotes: 0

Yu Zhang
Yu Zhang

Reputation: 2149

There are multiple ways that you can located an element using selenium and python. In your case, you could potentially use find_element_by_css_selector or find_element_by_xpath, your HTML code indentation is a bit hard to read, so I am assuming body encloses everthing and ul all li below it. I am using Chrome as my example, if you are using Firefox, it is natively supported by Selenium, which means you do not need to provide a Firefox path as I did for Chrome below.

chrome = webdriver.Chrome('PathToChromeDriver.exe')
element = chrome.find_element_by_xpath("//body//ul//li[@id='system']")

Or alternatively, you can find your element by asking for the 7th li element under ui, this time, I am using Firefox in my example.

firefox = webdriver.Firefox()
element = firefox.find_element_by_xpath("//body//ul//li[7]")

My personal favorite way to locate an element is via css selector:

firefox = webdriver.Firefox()
element = firefox.find_element_by_css_selector("li[id='system']")

In case you have two elements that share the same IDs, e.g. id = 'system', you can use css selector to fine select your element, for example:

firefox = webdriver.Firefox()
element = firefox.find_element_by_css_selector("li[id='system'][style='right: 0px']")

Basically, you can append as many attributes as you need to uniquely locate an element:

firefox = webdriver.Firefox()
element = firefox.find_element_by_css_selector("li[id='system'][style='right: 0px'][attribute3 = 'value'][attribute4 = 'value']")

Below are two links you may find helpful: http://www.w3schools.com/xsl/xpath_syntax.asp http://www.w3schools.com/cssref/css_selectors.asp

Upvotes: 0

alecxe
alecxe

Reputation: 474141

Find it by link text:

driver.find_element_by_link_text("SYSTEM")

You may need to wait for the element to be clickable also:

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
system = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "SYSTEM")))
system.click()

Upvotes: 4

Related Questions