Home > database >  How to click on a particular file in a web page whose filename is given on a text file using python-
How to click on a particular file in a web page whose filename is given on a text file using python-

Time:12-24

I have gdrive testing named folder in my goggle drive inside which 4 files are there . So ,I have a webpage where automatically this files are saved but I have saved names of 3 files in a text file in my local . Now How can i click on only those files in a web page whose names is written on the text file.

testing_file = os.path.sep.join((TestBase.current_directory,'src','Cloudconnector_files' , 'files_to_test.txt')) elem = Driver.find_elements(By.CLASS_NAME,'mat-checkbox-layout') with open(check_file,'r') as f: over = f.readlines() for i in over: res = i

    for x in Driver.find_elements(By.CLASS_NAME,'mat-checkbox-layout'):
        file_index = webdriver.ActionChains(Driver).move_to_element(x).click(res).perform()
    time.sleep(5)
Here , testing_file is the variable and files_to_test.txt is the file inside which the filenames are present which i want to click on a web page .

and class-name = mat-checkbox-layout is the locator of the small box near the filenames on the webpage where 4 files are present but out of this 4 boxes i only want to click 3 filename means 3 boxes not 4 boxes .

Expected : The filenames which is written on the .txt file that only files i want to click on a webpage

CodePudding user response:

Helpful Ways to Locate Elements in Selenium

You can also locate an element using the XPATH in Selenium. You can look for the XPATH in Chrome using the following helpful tutorial https://www.browserstack.com/guide/find-element-by-xpath-in-selenium

driver.find_element(By.ID, "id")
driver.find_element(By.NAME, "name")
driver.find_element(By.XPATH, "xpath")
driver.find_element(By.LINK_TEXT, "link text")
driver.find_element(By.PARTIAL_LINK_TEXT, "partial link text")
driver.find_element(By.TAG_NAME, "tag name")
driver.find_element(By.CLASS_NAME, "class name")
driver.find_element(By.CSS_SELECTOR, "css selector")

To learn more check out the official Python Selenium Documentation on Locating Elements: https://selenium-python.readthedocs.io/locating-elements.html

Best Approach

So, I would suggest working with above provided options and use the best one in your scenario. From what I understand you can use By.LINK_TEXT or By.PARTIAL_LINK_TEST if the website you are working with has a particular naming structure to files if it embeds the download links in the HTML Source. Can your provide the URL or HTML Source to the Page so I can better help you. But the above options of finding elements should help you determine those elements where there is a text file.

CodePudding user response:

You can right click on each file you want to click on using Selenium on the web page, then click on inspect and choose the HTML code for the files you want. Finally right click and hover over "Copy", then select "Copy selector"(CSS Selector), so you'll use By.CSS_SELECTOR and .click()

Example:-

file1 = driver.find_element(By.CSS_SELECTOR, 'css_selector_example')
file1.click()

I don't know if this helped or solved your problem, but it's hard to understand with this bit of information on the problem you have.

I hope it helps :)

CodePudding user response:

I only want to click on those files on web page whose names are written on a txt file on my local .

  • Related