Issue
I want to get the URL I am re-directed to when I click one button on the website, and still stay on the same page instead of being redirected. The button is not a simple hyperlink and will trigger a complex Javascript function.
click() does not work for me
I refer to Selenium get URL of "a" Tag without href attribute and Selenium: Getting ultimate href/link without clicking on it when it's a javascript call, which shows that the button has to be clicked to get the URL. Of course, I can click() the button and use 'driver.current_url', but in this way I am redirected to a new URL. I do not want the redirection since I have more things to do on the current page and want to stay on the same page. I can not use CTRL + click to open the new page in a new tab, either.
My idea
I read about how to find the source Javascript code (How to find what code is run by a button or element in Chrome using Developer Tools), but I found the codes too complex for me. Should I find the codes that produce the URL?
I wonder can I just "fork" the current page? Or something like simulating/executing the Javascript code in a separate sandbox? I am using python.
Solution
I finally found a solution. The idea is to modify the javascript codes.
Step 1: locate the javascript codes that cause the redirection.
Refer to this answer. Note that for me I also add Event Listener Breakpoints for the load event.
Step 2: modify the javascript codes and delete redirection
Just find the line where the new URL has been computed and is being directed to. Store the url in a global javascript variable target_url
.
Step 3: Use an extension to do the modification automatically in Selenium
See my answer. In a word, use the extension Resource Override.
Step 4: Selenium side, get the variable url
my_target_url = driver.execute_script("return target_url;")
Answered By - hellohawaii
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.