1

I have been learning selenium and Implementing It.I tried uploading files from the bot. During this I came to know the position(upload button content) keeps on changing on each reload. I managed to trace out option for .jpg file but I cannot track .pdf file uploading. Much detail on my code below.

<div _ngcontent-kkx-c10="" class="form-group document-upload" xpath="1">
    <div _ngcontent-kkx-c10="" class="document-upload-info">
        <span _ngcontent-kkx-c10="" class="document-type">
            KYC Form <!----><span _ngcontent-kkx-c10="" style="margin-left:5px;color:red;">* </span>
        </span>
    </div>
    <!----><!---->
    <label _ngcontent-kkx-c10="" class="btn btn-outline upload-button" style="border: 1px solid transparent; border-radius: 0.25rem; border-color: #0078D7; color: #0078D7 !important;">
        Upload <input _ngcontent-kkx-c10="" hidden="" multiple="" type="file" accept=".pdf">
    </label>
    <!----><!----><!---->
</div>


<div _ngcontent-kkx-c10="" class="form-group document-upload" xpath="1">
    <div _ngcontent-kkx-c10="" class="document-upload-info">
        <span _ngcontent-kkx-c10="" class="document-type">
            Photo  Form <!----><span _ngcontent-kkx-c10="" style="margin-left:5px;color:red;">* </span>
        </span>
    </div>
    <!----><!---->
    <label _ngcontent-kkx-c10="" class="btn btn-outline upload-button" style="border: 1px solid transparent; border-radius: 0.25rem; border-color: #0078D7; color: #0078D7 !important;">
        Upload <input _ngcontent-kkx-c10="" hidden="" multiple="" type="file" accept=".jpg">
    </label>
    <!----><!----><!---->
</div>

<div _ngcontent-kkx-c10="" class="form-group document-upload" xpath="1">
    <div _ngcontent-kkx-c10="" class="document-upload-info">
        <span _ngcontent-kkx-c10="" class="document-type">
            Citizenship   Form <!----><span _ngcontent-kkx-c10="" style="margin-left:5px;color:red;">* </span>
        </span>
    </div>
    <!----><!---->
    <label _ngcontent-kkx-c10="" class="btn btn-outline upload-button" style="border: 1px solid transparent; border-radius: 0.25rem; border-color: #0078D7; color: #0078D7 !important;">
        Upload <input _ngcontent-kkx-c10="" hidden="" multiple="" type="file" accept=".pdf">
    </label>
    <!----><!----><!---->
</div>

I did following way to upload image.

photo_filepath_input_box = driver.find_element_by_xpath("//input[@accept='.jpg']").send_keys(
                    '/home/daniel/Desktop/website.jpg')

Here during bot loading the above three Forms could appear in any position i.e Kyc form could arrive at the end (third position) or second and similar for rest. So I want to know If I could check condition like if KYC form text is presenet then i need to click the xpath right below it i.e Label Any hint on this ?

Here is second file upload issue.

(+) button click code

WebDriverWait(self.driver,5).until(EC.presence_of_element_located((By.XPATH,"//span[contains(text(),'Citizenship Certificates')]/../..//i[@class='fa fa-plus ' ]"))).click()

second upload citizen ship code.

self.driver.find_element_by_xpath("((//span[contains(text(),'Citizenship Certificates ')]/../..//input[@type='file' and(@accept='.pdf')])[2]").send_keys('/home/navaraj/Desktop/{}'.format(row[75]))
lord stock
  • 1,191
  • 5
  • 32

1 Answers1

2

To upload files by Selenium the better approach is to send the file path directly to the input element, not by clicking elements.
Here you have 2 forms containing upload .pdf files inputs and single input for uploading .jpg files.
So, to upload the .pdf file in KYC form you can do this: .
Let's say your file is located on the disk in "C://Downloads//kyc_pdf_file.pdf" you can do this:

kyc_pdf_file_path = "C://Downloads//kyc_pdf_file.pdf"
driver.find_element_by_xpath("//div[.//span[@class='document-type' and (contains(.,'KYC'))]]//input[@type='file' and(@accept='.pdf')]").send_keys(kyc_pdf_file_path)

Uploading file to Citizenship form:

citizenship_pdf_file_path = "C://Downloads//citizenship_pdf_file.pdf"
driver.find_element_by_xpath("//div[.//span[@class='document-type' and (contains(.,'Citizenship'))]]//input[@type='file' and(@accept='.pdf')]").send_keys(citizenship_pdf_file_path)

While uploading ".jpg" will be similarly:

jpg_file_path = "C://Downloads//jpg_file.jpg"
driver.find_element_by_xpath("//input[@type='file' and(@accept='.jpg')]").send_keys(jpg_file_path)

UPD
The XPath for the Citizenship Certificates pdf upload is

//span[contains(text(),'Citizenship Certificates ')]/../..//input[@type='file' and(@accept='.pdf')]

And for the KYC Form is

//span[contains(text(),'KYC Form')]/../..//input[@type='file' and(@accept='.pdf')]

Here is second file upload issue.

(+) button click code

WebDriverWait(self.driver,5).until(EC.presence_of_element_located((By.XPATH,"//span[contains(text(),'Citizenship Certificates')]/../..//i[@class='fa fa-plus ' ]"))).click()

second upload citizen ship code.

self.driver.find_element_by_xpath("((//span[contains(text(),'Citizenship Certificates ')]/../..//input[@type='file' and(@accept='.pdf')])[2]").send_keys('/home/navaraj/Desktop/{}'.format(row[75]))
lord stock
  • 1,191
  • 5
  • 32
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • How will I identify It is xpath for KYC or it is for CitizenShip? as I mention the uploading buttons keeps changing – lord stock Jul 29 '21 at 07:04
  • In my opinion we should check if the pdf is for KYC form or It is for CitizenShip Form only way to distinguish is to find text content. – lord stock Jul 29 '21 at 07:09
  • In the XML you presented I see no KYC or Citizenship so I can't distinguish between what I don't see. – Prophet Jul 29 '21 at 07:09
  • ` KYC Form * ` This is located here @Prophet – lord stock Jul 29 '21 at 07:10
  • OK, I see. So, both KYC and Citizenship forms are having upload files inputs all the times, just the order of their appearance is changing? – Prophet Jul 29 '21 at 07:15
  • Yeah Sometime Jpg file upload comes at top and some time KYC comes at top and even sometimes Citizen ship comes at top so I am unable to distinguish which pdf file i.e citizen or Kyc to be upoaded for pdf file. – lord stock Jul 29 '21 at 07:16
  • 1
    See if updated answer is what you asked for – Prophet Jul 29 '21 at 07:30
  • I came to realize that the xpath was not correct any solutin on this? – lord stock Aug 09 '21 at 16:14
  • What do you mean? – Prophet Aug 09 '21 at 16:36
  • Actually the xpath you answered in your answer is still not unique sir I also lately came to know about this. It provides 2 items for same xpath sir, – lord stock Aug 09 '21 at 17:09
  • 1
    Did you provide me a link to the website so I could find the correct and unique locator? – Prophet Aug 09 '21 at 17:58
  • the website link is `https://tms17.nepsetms.com.np/client-registration` please select client type = Individual on first page and directly headover document upload i.e page 6. And please notice that the upload button keeps changing its position on every reload. – lord stock Aug 10 '21 at 03:02
  • any Hint really stucked on the problem :( – lord stock Aug 10 '21 at 08:46
  • 1
    Updated the answer. Now it should work. You know, it's difficult to give correct locator without seeing the actual web page :) – Prophet Aug 10 '21 at 08:56
  • That worked like charm thank you so much for responding my comment – lord stock Aug 10 '21 at 09:07
  • can you please let me know `/../..//` about this or any documentation? – lord stock Aug 10 '21 at 09:08
  • `/..` is going one level up with xpath. – Prophet Aug 10 '21 at 09:12
  • https://stackoverflow.com/questions/38457563/xpath-get-element-above https://stackoverflow.com/questions/3676169/how-to-traverse-back-to-the-parent-node-in-the-xml-file-by-using-xpathusing-xsl – Prophet Aug 10 '21 at 09:15
  • I tried clicking second upload button i.e Citizenship Certificates using this xpath `(//span[contains(text(),'Citizenship Certificates ')]/../..//input[@type='file' and(@accept='.pdf')])[2]` this method but I dont know why in my code I am getting no element exception as the same xpath works fine in inspect in browser – lord stock Aug 12 '21 at 03:59
  • indexing does not this way? sir i tried to get second button by index – lord stock Aug 12 '21 at 09:15
  • There is no such element. `//span[contains(text(),'Citizenship Certificates ')]/../..//input[@type='file' and(@accept='.pdf')]` is an unique locator. There is no second element with such locator... – Prophet Aug 12 '21 at 11:26
  • we can find that xpath only after clicking (+) upload button in the link I provided and we will see that xpath.... does this mean xpath is not detected by this situation any solution to overcome this? – lord stock Aug 12 '21 at 11:52
  • Well, you didn't mention clicking `+` button before... After clicking `+` there will be `(//span[contains(text(),'Citizenship Certificates ')]/../..//input[@type='file' and(@accept='.pdf')])[2]` element there. Even `(//span[contains(text(),'Citizenship Certificates ')]/../..//input[@type='file' and(@accept='.pdf')])[5]` if you click `+` at least 5 times. I just saw that. But these elements are not visible and you have to wait for these elemems appearance after clicking the `+` button some time... – Prophet Aug 12 '21 at 12:00
  • I tried using Webdriver wait for around 10 sec I am always getting no element found exception in code while browser show unique xpath – lord stock Aug 12 '21 at 12:07
  • 1
    That's strange. I can't see your code, but if you wrote it correctly it should work – Prophet Aug 12 '21 at 12:11
  • sorry I edited your answer please ignore that edit I have added part of my code any suggestion regarding code? – lord stock Aug 12 '21 at 12:34
  • 1
    It's mainly for your use, so I don't care :) – Prophet Aug 12 '21 at 12:38
  • no i added second upload related code in my qsn any wrong in wait in code.? – lord stock Aug 12 '21 at 12:40
  • 1
    I trust you. In case this code worked for you it should be correct – Prophet Aug 12 '21 at 12:43
  • 1
    thank you so much for your help hope i can solve this :( – lord stock Aug 12 '21 at 12:45