1

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.

6
  • What is your business case? What do you want to do? Thanks. Commented May 5, 2020 at 12:34
  • i want to click a button whose text is "Select File" but until now i am still unable to click that...btw it is an iframe, do you know the solution for my problem? Commented May 5, 2020 at 13:28
  • Share your HTML or URL with iframe and so on. Commented May 5, 2020 at 13:38
  • You have to switch to the iframe first. Please add the HTML of the iframe. Commented May 5, 2020 at 13:53
  • i already added the iframe to my question, please review my question again Commented May 5, 2020 at 14:02

3 Answers 3

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

hello, it doesn't work on me anyway, i got timeout when i tried to use your code
0

Get the element using the inner text of the lable,

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

2 Comments

i get this : Unable to locate element: //label[contains(text(),'Select File')] do you have any other way besides that?
Can you wait for some time before this call?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.