Home > Enterprise >  How to click on this 'a' tag with selenium in python
How to click on this 'a' tag with selenium in python

Time:03-22

I have this element

<p><a onclick="return getpermission(this);" id="ctl00_ContentPlaceHolder1_btnSub"  data-endisbutton="" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$btnSub','')">▼ Taylor-Swift-Bad-Blood-ft.-Kendrick-Lamar.srt</a> <strong>Subtitle (.SRT) </strong></p>

on this page and I want to click on 'a' tag, so I found the 'a'tag element("elem") and ran (1st way) elem.click() then recieved

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <a onclick="return getpermission(this);" id="ctl00_ContentPlaceHolder1_btnSub"  data-endisbutton="" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$btnSub','')">...</a> is not clickable at point (185, 668). Other element would receive the click: <a href="https://www.rentanadviser.com/downloads/kripto_video_protector_setup.exe" rel="nofollow" target="_blank">...</a>
  (Session info: chrome=99.0.4844.82)

then ran (2nd way)

from selenium.webdriver.common.action_chains import ActionChains
action=ActionChains(driver)
action.move_to_element(elem)#or the line below
action.move_to_element_with_offset(elem,0,80)
action.click()
action.perform()

then got error:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=99.0.4844.82)

then did (3rd way) in which first element of elem1 is the elem(a tag)

driver.execute_script("arguments[0].click;", elem1)

which doesn't do anything.

btw this way I found 'elem'

links=driver.find_elements_by_tag_name("p")
    for i in range(len(links)):
        strong=links[i].find_elements_by_tag_name('strong')
        if l(strong)>0:
            if links[i].find_elements_by_tag_name('strong')[0].text=='Subtitle (.SRT)':
                elem=links[i].find_elements_by_tag_name('a')[0]

CodePudding user response:

If these buttons are what you are looking for: DOM Snapshot

If you want only the .srt button:

srt_btn = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='dowloadbuttons']//p//a[contains(text(), '.srt')]")))
driver.execute_script("arguments[0].click();", srt_btn)

If you want click all the 4 buttons:

all_btn = WebDriverWait(driver, 30).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@id='dowloadbuttons']//p//a[contains(@class, 'btn')]")))
for btn in all_btn:
    driver.execute_script("arguments[0].click();", btn)

CodePudding user response:

You need scroll the page to reach the element and then click on the element.

Use webdriverwait and wait for element to be clickable.

url = 'https://www.rentanadviser.com/subtitles/getsubtitle.aspx?artist=Taylor Swift&song=Bad Blood ft. Kendrick Lamar'
driver.get(url)
wait=WebDriverWait(driver,10)
button1=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#ctl00_ContentPlaceHolder1_btnSub")))
driver.execute_script("arguments[0].scrollIntoView();", button1)
button1.click()
print("clicked")
time.sleep(10) #just to check whether downloading

You need to import below library.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
  • Related