I have a problem with clicking an element using XPath in selenium. Here is the HTML element for the problem:
<label for="file" class="pb default" style="display: inline-block;margin: 5px 10px;">Select File</label>
Do you know the solution for this? Any response is really appreciated.
UPDATE:
here is the whole source code for the problem
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<BASE HREF="http://1.1.1.19/webclient/utility">
<link rel=stylesheet type="text/css" href="../webclient/skins/skins-20190807-1904/iot18/css/filex.css">
</head>
<body style="background: transparent; background-color: transparent">
<form name="IMPORT" id="IMPORT" enctype="multipart/form-data" method="post">
<input type="hidden" NAME="componetId" VALUE="itemimage_3_1-if">
<input type="hidden" NAME="controlId" VALUE="itemimage_3_1">
<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>
</form>
</body>
<script>
document.querySelector('#file').addEventListener('change', function(e){
document.querySelector('#fileName').value = e.currentTarget.files[0].name;
});
</script>
</html>
Can try with //label[@for='file']
or //label[@for='file'][text()='Select File']
If this is not working maybe you are targeting the wrong element or you are not waiting enough before it appears. When dealing with uploading files, I target the input with type file //input[@type='file'], not the label, you may need to provide bigger part of your HTML.
if class=pb default is a unique class in the web-page then try this out.
driver.driver.find_element(By.XPATH,'//*[@class="pb default"]').click()
or you can search by text Select File
driver.find_element(By.XPATH,'//*[contains(text(),"Select File")]').click()
Well No , you don't click on a <label>
element. However, you may require to locate the <label>
element for other purposes.
To identify the <label>
element you can use either of the following Locator Strategies :
Using css_selector :
element = driver.find_element(By.CSS_SELECTOR, "label.pb.default[for='file']")
Using xpath :
element = driver.find_element(By.XPATH, "//label[@class='pb default' and text()='Select File']")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.