I have this line of code that I want to click using Selenium WebDriver with Python, it is derived from an iframe:
<table width="100%" cellspacing="0" align="center" class="maintable">
<tr>
<td align="left" style="white-space: nowrap;">
<label for="file" class="pb default" style="display: inline-block;margin: 5px 10px;">Select File</label>
<input id="fileName" onmousedown="" type="text" value="" class="fld fld_ro text ib" readonly size="50"/>
<input id="file" type="file" name="value" title="Specify a New File" onchange="" size="35" class="text" style=" width: 0.1px;height: 0.1px !important;fileStyle: 0;overflow: hidden;position: absolute;z-index: -1;opacity: 0;" value="" onclick="if(!parent.undef(parent.firingControl) && parent.firingControl.id==this.id){parent.sendEvent('clientonly','clickFileButton', this.id)}">
</td>
</tr>
</table>
I want to click the "Select File" label (it is a button but without XPath or id) but I really am unable to do so...
this is the iframe :
<iframe id="upload_iframe" allowtransparency="true" class=" " role="presentation" tabindex="0" src="http://www.test.com/webclient/utility/uploadfile.jsp?controlId=itemimage_3_1&componentId=itemimage_3_1-if&uisessionid=1689" scrolling="no" marginheight="0" marginwidth="0" onfocus="setCurrentfocusId(event,this);" style="border:0px;" width="400" height="33" frameborder="0"> </iframe>
I have used this line of script to click the label but the button does not respond to my code:
button_element = browser.find_element_by_class_name('pb default')
button_element.click()
Do you know how to click that element? I am using firefox to do that... Any answer is appreciated, thx.
Try to use explicit wait before click action:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it(
(By.ID, "upload_iframe")))
browser.switch_to.frame(browser.find_element_by_id("upload_iframe"))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, ".pb.default"))).click()
To switch back to the original content use the following:
browser.switch_to.default_content()
I hope it helps you!
Get the element using the inner text of the lable,
driver.findElement(By.xpath("//label[contains(text(),'Select File')]")).click()
To access an iframe you have to switch to it with .switchTo().frame. See below. I commented out the switch back command.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(browser, 10).until(EC.presence_of_element_located(
(By.ID, ".pb.upload_iframe")))
browser.switchTo().frame("upload_iframe")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, ".pb.default"))).click()
#browser.switchTo().defaultContent()
Related
I have the following HTML code
<ul id="vmStateDropDown" class="dropdown-content active" style="white-space: nowrap; position: absolute; top: 83px; left: 264px; opacity: 1; display: block;"><li class="waves-effect waves-default" style="display: block;"><a class="" style="cursor: pointer;"><i class="material-icons left" style="cursor: pointer;">directions_run</i><span>Turn On</span></a></li><li class="waves-effect waves-default" style="display: block;"><a class="disabled" disabled="" style="cursor: pointer;"><i class="material-icons left" style="cursor: pointer;">power_settings_new</i><span>Turn Off</span></a></li><li class="waves-effect waves-default" style="display: block;"><a class="disabled" disabled="" style="cursor: pointer;"><i class="material-icons left" style="cursor: pointer;">loop</i><span>Restart</span></a></li></ul>
I need to click in the list item Turn On. Here is the code I have so far.
turn_vmon = driver.find_elements(By.XPATH,'/html/body/div[4]/main/div/div[3]/div[3]/div[1]/ul/li[1]')
When I print the text for turn_vmon variable, I can see the option which I need. However I am unable to figure out how to click on the list item.
Can someone please help?
To click on the element with text as Turn On you can use either of the following Locator Strategy:
Using xpath:
driver.find_element(By.XPATH, "//span[text()='Turn On']").click()
Ideally to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategy:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Turn On']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
<table _ngcontent-c19="" class="table">
<tbody _ngcontent-c19="">
<tr _ngcontent-c19=""><!---->
<td _ngcontent-c19="" class="ng-star-inserted">
<div _ngcontent-c19="" class="span3" style="height: auto;">
<div _ngcontent-c19="" class="text-center" style="visibility: visible;">
<button _ngcontent-c19="" class="b1" type="submit">Click This</button>
</div><!---->
</div>
</td>
</tr>
</tbody>
</table>
I'm trying to click a button inside a table using SELENIUM.
The Code I've Written is
driver.find_element_by_xpath("//*[contains(getText(),'Click This')]").click()
and
driver.find_element_by_class_name('b1').click()
they both throw an Element not Found Exception
you can try with this xpath :
wait = WebDriverWait(driver,30)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Click This') and #class='b1']"))).click()
Note that you will have to import :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Because your code runs faster than your browser that's why you need to tell him to wait until the element is visible and clickable.
button = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//button[text()='Click This']"))
button.click()
I m trying to select a dropdown arrow menu after Status.
I'm trying to build a Xpath to click this :
<a href="javascript:" class="btn btn3d selectionbtn" style="top:0px; left:136px; width:21px; height:21px;">
Xpath = driver.find_element_by_xpath('//*[id="WIN_3_7"]/div/a').click()
But it says that the
Unable to locate an element
Below is the HTML snippet of the above image.
<div id="WIN_3_7" arid="7" artype="EnumSel" ardbn="Status" arlbox="0,4,104,17" class="df arfid7 ardbnStatus EnumSel" style="z-index:1928;top:173px; left:705px; width:262px; height:21px;" arwindowid="3">
<label id="label7" class="label f9" for="x-arid_WIN_3_7" style="top:4px; left:0px; width:104px; height:17px;">Status*</label><div class="selection" style="top:0px; left:105px; width:157px; height:21px;" arselmenu="[{v:"Assigned"},{v:"Pending"},{v:"Waiting Approval"},{v:"Planning"},{v:"In Progress"},{v:"Completed"},{v:"Rejected"},{v:"Cancelled",l:"Canceled"},{v:"Closed"}]">
<input id="arid_WIN_3_7" type="text" class="text " readonly="" style="top:0px; left:0px; width:136px; height:21px;" title="Assigned">
<a href="javascript:" class="btn btn3d selectionbtn" style="top:0px; left:136px; width:21px; height:21px;">
<img class="btnimg" src="../../../../resources/images/mt_sprites.gif" alt="">
</a>
</div>
</div>
To click and expand the dropdown menu you can use the following line of code :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id="WIN_3_7"]/div[#class='selection']/a[#class='btn btn3d selectionbtn']/img[#class='btnimg']"))).click()
Update
As an alternative you can also try to click() on the <input> tag and expand the dropdown menu as follows :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='text' and #id='arid_WIN_3_7']"))).click()
Hey im using python selenium to download data from table. But when im want to prepare data i cant select a checkbox 'toggle all'....
im geting on page:
browser.get("https://gold.jgi.doe.gov/studies?setColumns=yes&Organism.NCBI+Taxonomy+ID=%3D500633")
click 'select columns for table'
browser.find_element_by_xpath('//*[#id="showColsButton"]').click()
and here we are checkbox ;toggle all'...
browser.find_element_by_xpath('//*[#id="selectFieldsList"]/thead/tr[2]/td/input').click()
Realy tryed xpath, css selector....
and here we are html fragment:
<table class="selectFieldsList" id="selectFieldsList">
<thead>
<tr><td colspan="2" align="center">
Select Fields using the Checkboxes<br>
<input type="submit" value="Submit" name="fieldSubmit" id="submitMe" class="submitMe">
</td></tr>
<!-- add a select all option -->
<script language="JavaScript">
function toggle(source) {
checkboxes = document.getElementsByName('selectField');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
<tr><td> <input type="checkbox" onclick="toggle(this)"> Toggle All<br> </td></tr>
<tr><td>* = required column</td><td> </td> </tr>
<tr><td> <input type="button" id="entityFieldSelectorToggle" value="Expand All Fields"> </td>
</tr></thead>
<tbody>
Instead of using sleep use explicit wait is good practise, because it will same 5 sec of time even after element is found. Hope it will be useful to you
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver.get("https://gold.jgi.doe.gov/studies?setColumns=yes&Organism.NCBI+Taxonomy+ID=%3D500633")
driver.find_element_by_xpath('//*[#id="showColsButton"]').click()
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="selectFieldsList"]/thead/tr[2]/td/input')))
driver.find_element_by_xpath('//*[#id="selectFieldsList"]/thead/tr[2]/td/input').click()
There is drop menu in a frame, in code it starts with <div tag id=masterdiv, div class=submenu and onclick="SwitchMenu('sub1')">,
I have to click first on Personal Info., and then select Personal Details
I tried
driver.find_element_by_xpath("//a[contains(href,'PersonalInfo/EmpPersonalInfo.jsp')
and
contains(#class,'submenu')]")
and
driver.find_element_by_link_text("//a [#href=\"../EmployeeFiles/AcademicInfo/DailyStudentAttendanceEntry.jsp\"]")
None works.
<div id="masterdiv">
<div class="menutitle" onclick="SwitchMenu('sub1')">Personal Info.  <img src="../Images/arrow.gif"></div>
<span class="submenu" id="sub1" style="display: none;">
<img src="../Images/bullet4.gif"> <a target="DetailSection" title="Personal Information" href="PersonalInfo/EmpPersonalInfo.jsp"><font face="Arial" color="white" size="2">Personal Detail</font></a><br>
<img src="../Images/bullet4.gif"> <a target="DetailSection" title="Personal Information" href="PersonalInfo/imprestAcc.jsp"><font face="Arial" color="white" size="2">My Imprest Account</font></a><br>
<img src="../Images/bullet4.gif"> <a target="DetailSection" title="Self Attendance Detail" href="PersonalInfo\SelfAttendanceDetail.jsp"><font face="Arial" color="white" size="2">Self Attendance</font></a><br>
<img src="../Images/bullet4.gif"> <a target="DetailSection" title="Change eMailID/Contact Numbers" href="PersonalInfo/EmpModifyEmailIDTelephone.jsp"><font face="Arial" color="white" size="2">Edit Info.</font></a><br> }
Actually your provided locator looks incorrect that's why are unable to find element, you should try using WebDriverWait to wait until element present as below :-
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#first switch to frame if present
driver.switch_to_frame("frame id or name")
wait = WebDriverWait(driver, 10)
# now find menu and click
menu = wait.until(EC.presence_of_element_located((By.XPATH, ".//*[contains(., 'Personal Info')]")))
menu.click()
#now find submemu and click
submemu = wait.until(EC.presence_of_element_located((By.XPATH, ".//*[contains(., 'Personal Detail')]")))
submemu.click()