Search code examples
pythonhtmlselenium-webdriverwebdriver

Using selenium want to click and URL defined in a class


I need another hint today. I'm trying to build a Python/Selenium code, the Idea is to click on www.thewebsiteIwantoclickon Below is the sample of the HTML I am working on.

The class entity-result__title-text is repeated several times in the HTML, so I want to click for each class entity-result__title-text the element href= open the site www.thewebsiteIwantoclickon perform some actions (I have in a separate code) and go back to the previous HTML and repeat the same process until the last class entity-result__title-text

 <span class="entity-result__title-text
            t-16">
            <a class="app-aware-link " href="https://www.thewebsiteIwantoclickon" data- 
 test-app-aware-link="">
              <span dir="ltr"><span aria-hidden="true"><!---->Mi Name<!----></span><span class="visually-hidden"><!---->See something<!----></span></span>
            </a>
            <span class="entity-result__badge t-14 t-normal t-black--light">
              <div class="display-flex
        flex-row-reverse
        align-items-baseline">
    <!---->    <span class="image-text-lockup__text entity-result__badge-text">
          <span aria-hidden="true"><!---->• 2º<!----></span><span class="visually-hidden"><!---->example<!----></span>
         </span>
      </div>
            </span>
        </span>

I have written the following code, but no action is performed.

links = driver.find_elements(By.XPATH, "//span[@class='entity-result__title-text']/a[@class='app-aware-link']")
for link in links:
    href = link.get_attribute("href")
    link.click()
    # My Action done and I'm ready to close the website
    
    driver.back() 

But nothing happens.


Solution

  • To create a list of the desired elements you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      links = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "span.entity-result__title-text > a.app-aware-link")))
      
    • Using XPATH:

      links = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[contains(@class, 'entity-result__title-text ')]/a[@class='app-aware-link']")))