Yogesh Tyagi Yogesh Tyagi - 2 months ago 6
Python Question

Not able to click on link in webpage using selenium webdriver

I want to click on "Wireless" link in my router web page using selenium python webdriver. Below is the html:

<html>
<head>
<frameset framespacing="0" frameborder="0" border="0" rows="50,*,15">
<frame noresize="" marginheight="0" marginwidth="0" target="_self" scrolling="no" border="0" frameborder="no" name="logofrm" src="logo.html">
<frameset border="0" frameborder="0" cols="170,*">
<frame noresize="" marginheight="0" marginwidth="0" target="_self" scrolling="no" border="0" frameborder="no" name="menufrm" src="menu.html">
<html>
<head>
<body class="mainMenuBody" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0">
<table cellspacing="0" cellpadding="0" border="0" height="100%">
<tbody>
<tr>
<td width="1" style="background-color: #C35822"></td>
<td class="menu" width="170" valign="top" align="left">
<br>
<script language="javascript">
<div id="folder0">
<div id="folder1" style="display: block;">
<div id="folder2" style="display: none;">
<div id="folder3" style="display: none;">
<div id="folder4" style="display: none;">
<div id="item5" style="display: none;">
<div id="item6" style="display: none;">
<div id="item7" style="display: none;">
<div id="item8" style="display: none;">
<div id="folder9" style="display: none;">
<div id="folder10" style="display: none;">
<div id="folder11" style="display: none;">
<div id="folder12" style="display: block;">
<div id="folder13" style="display: none;">
<div id="item14" style="display: none;">
<div id="item15" style="display: none;">
<div id="item16" style="display: none;">
<div id="folder17" style="display: none;">
<div id="item18" style="display: none;">
<div id="folder19" style="display: none;">
<div id="item20" style="display: none;">
<div id="item21" style="display: none;">
<div id="item22" style="display: none;">
<div id="folder23" style="display: none;">
<div id="folder24" style="display: none;">
<div id="item25" style="display: none;">
<div id="item26" style="display: none;">
<div id="folder27" style="display: none;">
<div id="folder28" style="display: none;">
<div id="folder29" style="display: none;">
<div id="folder30" style="display: none;">
<div id="folder31" style="display: none;">
<div id="folder32" style="display: none;">
<div id="item33" style="display: none;">
<div id="item34" style="display: none;">
<div id="folder35" style="display: none;">
<div id="item36" style="display: none;">
<div id="item37" style="display: none;">
<div id="item38" style="display: none;">
<div id="item39" style="display: none;">
<div id="folder40" style="display: none;">
<div id="item41" style="display: none;">
<div id="item42" style="display: none;">
<div id="folder43" style="display: none;">
<div id="folder44" style="display: none;">
<div id="folder45" style="display: none;">
<div id="folder46" style="display: none;">
<div id="folder47" style="display: none;">
<div id="item48" style="display: none;">
<div id="item49" style="display: none;">
<div id="item50" style="display: none;">
<div id="item51" style="display: none;">
<div id="folder52" style="display: none;">
<div id="folder53" style="display: block;">
<table cellspacing="2" cellpadding="0" border="0" width="100%">
<tbody>
<tr>
<td class="menuCell" width="100%" valign="middle" nowrap="">
<a class="menuLink" onclick="javascript:clickOnFolder(53)" target="basefrm" href="wlswitchinterface0.wl"> Wireless </a>
</td>
</tr>
</tbody>
</table>
</div>
<div id="item54" style="display: block;">
<div id="item55" style="display: block;">
<div id="item56" style="display: block;">
<div id="item57" style="display: block;">
<div id="item58" style="display: block;">
<div id="item59" style="display: block;">
<div id="folder60" style="display: block;">
<div id="folder61" style="display: block;">
<div id="folder62" style="display: none;">
<div id="item63" style="display: none;">
<div id="item64" style="display: none;">
<div id="item65" style="display: none;">
<div id="folder66" style="display: none;">
<div id="folder67" style="display: none;">
<div id="folder68" style="display: none;">
<div id="folder69" style="display: none;">
<div id="folder70" style="display: none;">
<div id="item71" style="display: none;">
<div id="item72" style="display: none;">
<div id="folder73" style="display: none;">
<div id="folder74" style="display: none;">
<div id="folder75" style="display: none;">
<noscript>You must enable JavaScript in your browser.</noscript>
</td>
<td width="1" style="background-color: #C35822"></td>
<td style="background-color: #ffffff"></td>
</tr>
</tbody>
</table>
</body>
</html>
</frame>
<frame noresize="" marginheight="16" marginwidth="0" target="_self" scrolling="auto" border="0" frameborder="no" name="basefrm" src="info.html">
</frameset>
<frame noresize="" marginheight="0" marginwidth="0" target="_self" scrolling="no" border="0" frameborder="no" name="footerfrm" src="footer.html">
</frameset>
</html>


I have following line in my script:

driver.find_element_by_xpath('//a[@href="wlswitchinterface0.wl"]').click()


But it's not working and throwing NoSuchElementException


NoSuchElementException: Message: Unable to locate element:
{"method":"xpath","selector":"//a[@href=\"wlswitchinterface0.wl\"]"}

Answer

The problem is not the quotes, but is because you're inside a frame.

First, you need to switch inside his:

frame = driver.find_element_by_xpath("//frame[@name='menufrm']")
driver.switch_to_frame(frame)

Now, you can access your link:

driver.find_element_by_xpath("//a[@href='wlswitchinterface0.wl']").click()

After you access all you need inside this frame, you need switch to default content:

driver.switch_to_default_content()