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=""
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Jerome
  • 39
  • 1
  • 8
  • 1
    Change _where_? Post your existing code. – Selcuk Sep 15 '20 at 02:29
  • We don't even know what language you are working with. You have only posted a HTML tag. – Selcuk Sep 15 '20 at 02:30
  • The Title says: Python and I added the tags – Jerome Sep 15 '20 at 02:33
  • Not change like I posted: cut , it out... so with python I need to cut out of the HTMl source code this values. – Jerome Sep 15 '20 at 02:35
  • You have also tagged it `javascript` and `selenium`. There is no Python code in your question either. See https://stackoverflow.com/help/minimal-reproducible-example and https://stackoverflow.com/help/how-to-ask first. – Selcuk Sep 15 '20 at 02:36
  • @Jerome 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. – JeffC Sep 15 '20 at 04:51

2 Answers2

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
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)
JeffC
  • 22,180
  • 5
  • 32
  • 55