DeadlyKitten999
DeadlyKitten999

Reputation: 67

Unable to click label element using Selenium + Python

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&amp;componentId=itemimage_3_1-if&amp;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.

Upvotes: 1

Views: 1750

Answers (3)

Ratmir Asanov
Ratmir Asanov

Reputation: 6459

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!

Upvotes: 1

Jortega
Jortega

Reputation: 3790

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()

Upvotes: 0

ptsivakumar
ptsivakumar

Reputation: 437

Get the element using the inner text of the lable,

driver.findElement(By.xpath("//label[contains(text(),'Select File')]")).click()

Upvotes: 0

Related Questions