1

I’m trying to click an Upload from my Computer button on a page that has the source below.

I’m using selenium and tried several different approaches. The past failed approaches are commented out below, along with the current failed approach. The error that’s returned with the current approach is below.

Can anyone see what the issue might be and suggest how to solve it? I’m new to selenium so if someone can provide some explanation of what the html is doing and how their code solves the issue as well it would be really helpful for my understanding.

HTML code of the button:

<div class="hidden-xs">
    <label for="fuUploadFromMyComputer" class="hidden">
        Upload from my Computer
    </label>
    <input id="fuUploadFromMyComputer" type="file" name="upload">
    <button id="btnUploadFromMyComputer" 
            class="center-block btn btn-white-fill btn-block " 
            data-resume-type="COMPUTER" type="submit">
        <i class="zmdi zmdi-desktop-mac"></i>
        Upload from my Computer
    </button>
</div>

attempts:

# clicking upload button

# upload_btn = driver.find_element_by_id("fuUploadFromMyComputer")
# upload_btn = driver.find_element_by_css_selector(
#                 '.center-block.btn.btn-white-fill.btn-block')
# upload_btn = driver.find_element_by_link_text('Upload from my Computer')

# upload_btn.click()



from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
     (By.CSS_SELECTOR, "div.center-block btn.btn-white-fill.btn-block"))).click()

error:

---------------------------------------------------------------------------
TimeoutException                          Traceback (most recent call last)
<ipython-input-43-8fd80ff3c690> in <module>()
     14 from selenium.webdriver.support import expected_conditions as EC
     15 
---> 16 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.center-block btn.btn-white-fill.btn-block"))).click()
     17 
     18 time.sleep(3)

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/support/wait.py in until(self, method, message)
     78             if time.time() > end_time:
     79                 break
---> 80         raise TimeoutException(message, screen, stacktrace)
     81 
     82     def until_not(self, method, message=''):

TimeoutException: Message: 
1
  • My guess is that button is in an IFRAME... have you checked? The id, btnUploadFromMyComputer, should work if you click it. That wasn't one of your attempts. Commented Feb 22, 2019 at 5:56

3 Answers 3

1

To click on the element with text as Upload from my Computer you need to induce WebDriverwait for the element to be clickable and you can use either of the following solutions:

  • CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.center-block.btn.btn-white-fill.btn-block#btnUploadFromMyComputer"))).click()
    
  • XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='center-block btn btn-white-fill btn-block ' and @id='btnUploadFromMyComputer']"))).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
    
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, your suggestion with the css_selector worked. It then brings up a pop up box for me to select a file from to upload. Any suggestions on how to pass a file path to upload a file? Also why did you add the "#btnUploadFromMyComputer" to the css_selector? I'm still pretty new to selenium.
@modLmakur In css_selector while classes are expressed with the . character, the id attributes are denoted with the # character. If it is a pop up box to select a file from to upload ideally, you have to induce WebDriverWait again with EC as element_to_be_clickable(). Feel free to raise a new question with your new requirement.
1

Selenium's click() does not support to operate on invisible element. Thus please double confirm the button is visible or not when your code intend to click it.

If the button is not visible, how do you click it hands-on? Thus change your script to following the human steps to make the button visible before you can click it.

Back to your failure on below code

WebDriverWait(driver, 20).until(EC.element_to_be_clickable(
     (By.CSS_SELECTOR, "div.center-block btn.btn-white-fill.btn-block"))).click()

The reason is you give a wrong css selector which can't find any element from the page util reach the waiting timeout.

The correct css selector of the button can be any one of following:

  • button.center-block.btn.btn-white-fill.btn-block
  • button#btnUploadFromMyComputer

Comments

0

For C#, I used IJavaScriptExecutor to click on element. You may search this solution for Python syntax

public static void scrollElementToClick(IWebDriver driver, IWebElement element)
{
     IJavaScriptExecutor ex = (IJavaScriptExecutor)driver;
     ex.ExecuteScript("arguments[0].click();", element);
}

2 Comments

@halfer Anyway, i have banned for asking question [almost 1 year]. I have try to edit , improve , and delete duplicated question that cause of my dumb behavior .However, i still not be able to ask question. Do you have any recommendation ?. I suffer from it for a long time and wait for moderator to take a look.
Yep, plenty of recommendations here. Good luck! (For what it's worth, I think question bans and answer bans are calculated separately, so upvotes on answers would not necessarily unban you from asking questions).

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.