Python - How to edit href attribute in source code before clicking using Selenium
Asked Answered
S

2

0

I have a project where I always need to change the same values (cut them out):

<a class="sc-chPdSV iZXvhe sc-htpNat gEZjyJ" type="submit" name="audio-download" href="http:///www.website.com/get_audio/?session_token=6355f60252138cfd6.7872112701&amp;analytics_tier=undefined&amp;r=us-east-1&amp;game=0&amp;language=de&amp;d=1" target="_blank">Geräusch als Audio-Datei herunterladen</a>

I need to change of this code before clicking it:

de&amp;d=1

to

en

and

   target="_blank"

to

target=""
Sloane answered 15/9, 2020 at 2:28 Comment(6)
Change where? Post your existing code.Twink
We don't even know what language you are working with. You have only posted a HTML tag.Twink
The Title says: Python and I added the tagsSloane
Not change like I posted: cut , it out... so with python I need to cut out of the HTMl source code this values.Sloane
You have also tagged it javascript and selenium. There is no Python code in your question either. See stackoverflow.com/help/minimal-reproducible-example and stackoverflow.com/help/how-to-ask first.Twink
@Sloane The point people are trying to make is that you are expected to show effort. You can google these commands like the rest of us can and at least make a reasonable stab at a code attempt, written in python like your question and tag implies. Also, you added the JS tag but don't otherwise reference JS in your question. Don't make us guess what you want... edit your question and make it clear.Collegium
A
1

To change the de&amp;d=1 part of the href attribute as en you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[href^='http:///www.website.com/get_audio'][name='audio-download']")))
    browser.execute_script("arguments[0].setAttribute('href','http:///www.website.com/get_audio/?session_token=6355f60252138cfd6.7872112701&amp;analytics_tier=undefined&amp;r=us-east-1&amp;game=0&amp;language=en')", element)
    
  • Using XPATH and in a single line:

    browser.execute_script("arguments[0].setAttribute('href','http:///www.website.com/get_audio/?session_token=6355f60252138cfd6.7872112701&amp;analytics_tier=undefined&amp;r=us-east-1&amp;game=0&amp;language=en')", WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@name='audio-download' and text()='Geräusch als Audio-Datei herunterladen']"))))
    

To change the attribute target="_blank" as target="" you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[href^='http:///www.website.com/get_audio'][name='audio-download']")))
    browser.execute_script("arguments[0].setAttribute('target','')", element)
    
  • Using XPATH and in a single line:

    browser.execute_script("arguments[0].setAttribute('target','')", WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@name='audio-download' and text()='Geräusch als Audio-Datei herunterladen']"))))
    

References

You can find a couple of relevant detailed discussions in:

Asel answered 17/9, 2020 at 11:50 Comment(0)
C
0

You can do this without injecting the modified URL into the A tag on the page. You can just grab the URL, modify it using urllib.parse, and then just navigate to the modified URL. In the code below I used urllib.parse to unpack the various parts of the URL, make your desired changes, and then reassemble the URL.

from urllib.parse import urlparse, parse_qs, urlencode, urlunparse

old_url = 'http:///www.website.com/get_audio/?session_token=6355f60252138cfd6.7872112701&amp;analytics_tier=undefined&amp;r=us-east-1&amp;game=0&amp;language=de&amp;d=1'
o = list(urlparse(old_url))
q = parse_qs(o[4])
q['language'] = 'en'
del(q['d'])
o[4] = urlencode(q, doseq=True)
new_url = urlunparse(o)
print(new_url)
driver.get(new_url)
Collegium answered 15/9, 2020 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.