1

I'm currently working on a project with Selenium and bs4. So far so good I can navigate through the website using selenium without any problem however, I would need to click on a specific icon that opens a new tab with some information that I would need to scrape using BS4. My issue is when looking at the HTML I have no idea what to locate with Selenium in order to click on that icon.

HTML sample:

<a onmouseover="XYZ.Util.showHoverText(this, 'Print view')" href=
"app/exports/timesheet_print.php?startDate=2020-09-07&amp;endDate=2020-09-11&amp;filter_user=110483" target="_blank">
<img src="images/icons/printer.png" class="icon">
</a>

Usually I'm locating items by name, ID or Class but in that case I don't know what to chose from.

Should I look for the xpath instead ?

Vincent Aury
  • 195
  • 6

1 Answers1

0

To click on the icon you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href^='app/exports/timesheet_print']>img.icon[src^='images/icons/printer']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@href, 'app/exports/timesheet_print')]/img[@class='icon' and starts-with(@src, 'images/icons/printer')]"))).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