python-3.xxpathsplinter

Fill a table or list of values in textarea in website using Splinter


I am trying to write a script to automate the search for multiple entries. Here is the input:

<mirna-1>
<mirna-2>
<mirna-3>
    :    
    :   

The website address is https://www.mirnet.ca/miRNet/faces/upload/MirUploadView.xhtml

How can I fill mirna-1,.... into miRNA list box. I have made the following attempts but failed:

gecko = os.path.normpath('~/geckodriver')
nirnet_Results = []`
browser = Browser(executable_path=gecko)
browser.visit('https://www.mirnet.ca/miRNet/faces/home.xhtml')

# click miRNA_list
xpath = '//*[@id="j_idt39:j_idt55"]'
browser.find_by_xpath(xpath).click()
time.sleep(1)

#Select Human
xpath = '//*[@id="form:j_idt34"]/div[3]/span'
browser.find_by_xpath(xpath).click()
time.sleep(1)
xpath = '//*[@id="form:j_idt34_1"]'
browser.find_by_xpath(xpath).click()
time.sleep(1)

# select miRBase ID type
xpath = '//*[@id="form:j_idt38"]/div[3]/span'
browser.find_by_xpath(xpath).click()
time.sleep(1)
xpath = '//*[@id="form:j_idt38_1"]'
browser.find_by_xpath(xpath).click()
time.sleep(1)

# select Tissue type
xpath = '//*[@id="form:sourceOpt"]/div[3]/span'
browser.find_by_xpath(xpath).click()
time.sleep(1)
xpath = '//*[@id="form:sourceOpt_1"]'
browser.find_by_xpath(xpath).click()
time.sleep(1)

# select target type
xpath = '//*[@id="form:targetOpts"]/div[3]/span'
browser.find_by_xpath(xpath).click()
time.sleep(1)
xpath = '//*[@id="form:targetOpts_1"]'
browser.find_by_xpath(xpath).click()
time.sleep(1)

# Fill miRNA list
xpath = '//*[@id="form:listData"]'
browser.find_by_xpath(xpath).click()
time.sleep(1)
for mir in known_mir:
    browser.fill('form:listData',mir)
    browser.fill('form:listData','\n')

I am getting success in selecting options in starting drop-down list and when I try to paste a list of names in the miRNA list, I am getting all the names like this:

<mirna-1><mirna-2><mirna-3>....

while the right format is each name should in the next line. How to do this. Kindly suggest. Thanks.


Solution

  • Although I would recommend selenium for something like this, it seems you want to stick with splinter. The following fix using splinter. When reproducing this issue, each time browser.fill() is run it replaces the text already written. Since you didn't include what known_mir was in your code, I am assuming it is a list (correct me if I'm wrong). I tried combining what you wanted to put into the form and it worked.

    Replace:

    for mir in known_mir:
    
        browser.fill('form:listData',mir)
    
        browser.fill('form:listData','\n')
    

    With:

    browser.fill('form:listData', '\n'.join(mir for mir in known_mir))
    

    All my code does is combine each of the strings in known_mir.