0

So I have page structured like this :

<ul>
    <li>
        <div>
            <div>
                <span class="buttonUpload" custom="call.js">Button Upload</span>
                <input class="fa-cloud-upload" name="file" type="file" accept="">
            </div>
        </div>
    </li>
    <li>
        <div>
            <div>
                <span class="buttonUpload" custom="call.js">Button Upload</span>
                <input class="fa-cloud-upload" name="file" type="file" accept="">
            </div>
        </div>
    </li>
    <li>
        <div>
            <div>
                <span class="buttonUpload" custom="call.js">Button Upload</span>
                <input class="fa-cloud-upload" name="file" type="file" accept="">
            </div>
        </div>
    </li>
</ul>

I want to upload file only to the first input tag using send_keys() from selenium but for some reason it's not working, if there's only single li tags shown after refresh then the codes work, my code as bellow :


view_videos = WebDriverWait(driver, self.latency).until(EC.visibility_of_all_elements_located(
                (By.CSS_SELECTOR, "a[i18n='i18n.openLink']")))

for i in range(len(view_videos)):
    # some other script here

    uploadme = "/home/user/myfile.png"

    #script to upload
    driver.find_element_by_css_selector("input[class='fa-cloud-upload']").send_keys(str(uploadme))

I even try with driver wait and select all element with selected array:

#script to upload
upload_first = WebDriverWait(driver, self.latency).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "input[class='fa-cloud-upload']")))
         
upload_first[0].send_keys(str(uploadme))

But nothing happened, I execute also javascript to erase 2 <li> tags after the first one before upload but still upload not working, nothing happens, I just want to upload to first input only using selenium, any clue?

Rikudo Pain
  • 441
  • 9
  • 22

2 Answers2

0

Try using By.CLASS_NAME, 'fa-cloud-upload' or EC.presense_of_element_located((By.XPATH, 'its xpath')) You can find Xpath of the element in F12 panel. Inspect the element, then right click on it in F12 panel and copy -> Xpath.

MetraDZ
  • 39
  • 6
  • I tried using your suggestion by using presence of element and use xpath (I tried xpath in chrome dev tool and it located the input element I want) when driver wait I get the correct class (I print the class to prove) but upload not happened, any other clue? – Rikudo Pain Sep 07 '20 at 10:40
0

To send a character sequence to the first <input> tag instead of presence_of_all_elements_located() you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ul > li input.fa-cloud-upload"))).send_keys(str(uploadme))
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul/li//input[@class='fa-cloud-upload']"))).send_keys(str(uploadme))
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I can detect the input using presence of element but send keys not working (I can confirm it by print the class), using element to be clickable I always got selenium time out exception any other way? – Rikudo Pain Sep 07 '20 at 11:47
  • After checking, there are JS script that called after i press "Button Upload" to show upload window, is this have any effect? – Rikudo Pain Sep 07 '20 at 12:47
  • @RikudoPain Can you just copy, paste, execute the code and let me know the ststus please? – undetected Selenium Sep 08 '20 at 07:27
  • copy paste using css selector return error raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – Rikudo Pain Sep 08 '20 at 09:39
  • copy paste using xpath result in the same raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: – Rikudo Pain Sep 08 '20 at 09:43