search

Home  >  Q&A  >  body text

Using selenium want to click and define URL in class

I need another tip today. I'm trying to build Python/Selenium code with the idea of ​​clicking on www.thewebsiteIwantoclickon Below is an example of the HTML I'm working on.

Classentity-result__title-text is repeated multiple times in HTML, so I want to click the element href=## for each class entity-result__title-text # Open the website www.thewebsiteIwantoclickon Perform some actions (I did it in separate code) and go back to the previous HTML and repeat the same process until the last class Entity-Result__TitleText

<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 wrote the following code but it does nothing.

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 happened.

P粉904191507P粉904191507227 days ago3629

reply all(1)I'll reply

  • P粉966979765

    P粉9669797652024-04-05 09:14:49

    To create a list of required elements, you must be visibility_of_all_elements_ located(), you can use any of the following locator strategies:

    • Use 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")))
    • USEXPATH:

      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']")))

    reply
    0
  • Cancelreply