-1

I am trying to test downloading a file from a website using selenium on python.

The website has peculiar design where the file name appears as a text element above the button to download the file. There are no specific names or IDs for these buttons. And they are not known to us. So, I can't specify the ID or element name in the code directly.

Here is the HTML snippet:

<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 noborderBottom semiBolder-label ">
                    <span data-bind="text: jurisdictionName, attr: { id: jurisdictionId() + '-guides' }, visible: showInList" id="67-guides">Greece</span>
                    &nbsp;
                </div>
<div class="clearfix visible-xs"></div>
<div class="col-xs-6 col-sm-6  col-md-5 col-lg-5 text-center">
                     <div class="greenPDFIcon cursorPointer align-center" data-bind="event: { click: onHighlightClick.bind($data) }, style: { 'visibility': highLightUrl() ? 'visible' : 'hidden' }" style="visibility: visible;"></div>
                    <span class="taxGuidesText lg-visible md-visible xs-visible" data-bind="style: { 'visibility': highLightUrl() ? 'visible' : 'hidden' }" style="visibility: visible;">Highlights</span>
                    <!--<span class="taxGuidesText lg-visible md-visible xs-visible" data-bind="visible:showInList">Highlights</span>-->
                   
                </div>

Now, I first need to search for the text "Greece" in the above example.

Get it's location on the webpage:

class="col-xs-12 col-sm-12 col-md-12 col-lg-12 noborderBottom semiBolder-label"

Locate the button right below this text - so in the above example that gives me:

class="col-xs-6 col-sm-6  col-md-5 col-lg-5"

And then click on the button:

class="greenPDFIcon cursorPointer align-center"

The thing is, I do not know this "Greece". That comes through input parameter.

I only know that if the input parameter text is found on the webpage, the button will be right below it. And I have to click it to open the pdf file.

How to do that using selenium on python?

So far I have reached:

s=Service(r"driver_path")
browser = webdriver.Edge(service=s)
browser.get('webpage_url')
country = input('Enter a country name: ')

Also, suggest if I should use anything else rather than selenium to do this, as I understand this is more of web-scrapping than automated testing. I also tried beautifulsoup, but the website is not accessible directly through api. Browser access is required.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Meet
  • 461
  • 4
  • 19

1 Answers1

1

To locate the element with the country name e.g. Greece and click on the respective element with text as Highlights you can use the following Locator Strategies:

  • Using XPATH and "Old Style" String Formatting (% Operator):

    browser.get('webpage_url')
    country = input('Enter a country name: ')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'semiBolder-label')]//span[contains(., '%s')]//following::div[2]//span[contains(., 'Highlights')]" % country))).click()
    
  • Using XPATH and "New Style" String Formatting (str.format):

    browser.get('webpage_url')
    country = input('Enter a country name: ')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'semiBolder-label')]//span[contains(., '{}')]//following::div[2]//span[contains(., 'Highlights')]".format(country)))).click()
    
  • Using XPATH and String Interpolation / f-Strings (Python 3.6+):

    browser.get('webpage_url')
    country = input('Enter a country name: ')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, f"//div[contains(@class, 'semiBolder-label')]//span[contains(., '{country}')]//following::div[2]//span[contains(., 'Highlights')]"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hey thanks for such organized response. Just an additional query: I updated the code and ran it, it worked. But sometimes, it gives out an error saying the element is uninteractable. I modified the time from 20 to 30 seconds. What I observed is that the page changes that element from
    to
    after click on first element.
    – Meet Jan 19 '22 at 11:25
  • @Meet You are clicking on **Highlights** with respect to the text **Greece** only once before the change, so no harm if the element changes from `
    ` to `
    ` after click, agree?
    – undetected Selenium Jan 19 '22 at 11:27
  • True. This is just for my knowledge. Take an example of a login page. If the page shows only user name first and then upon entering username and click of enter shows the password field, but the html file has the password element set as `id='password'` from the start. The only change is adding `style` to its `
    `, will the code for a click operation on password field work?
    – Meet Jan 19 '22 at 11:36
  • 1
    Let's discuss the issue in [Selenium](https://chat.stackoverflow.com/rooms/223360/selenium) room. – undetected Selenium Jan 19 '22 at 11:36