0

I've encountered a problem

I searched this on google https://www.google.com/search?q=sen%20do&uule=w+CAIQICIZSG8gQ2hpIE1pbmggQ2l0eSwgVmlldG5hbQ%3D%3D and manual clicked on the first result (this link is attached with ads), I got the current url https://www.sendo.vn/?utm_source=google&utm_medium=affiliate&utm_campaign=333629566-13485316726&gclid=EAIaIQobChMI9eKR_YCh9AIVCz5gCh2ang22EAAYASAAEgI80_D_BwE

Did the same with the above link, used the code below but it only returned https://www.sendo.vn/ (Missing the params)

Here is the code

driver.get("${the_above_google_search_url}")

// Find first result element
Web firstResultElement = driver.findElement(By.className("iUh30"))

firstResultElement.click()

String currentUrl = driver.getCurrentUrl()

Any solution to get full url path with Selenium like manual click?

  • To be more accurate, change the question title to something like: "How handle 302 redirects on selenium with java?" Or "How get the final url after redirect with selenium and java?" – JRichardsz Nov 18 '21 at 04:34

1 Answers1

0

I entered to your web and inspecting the page I saw that there are two redirects (status 302) after the click on the first result on google search:

redirects

And the second redirect has the url that you are looking for in the header location because is a redirect (302):

enter image description here

If you wait, the browser execute the second redirect and the url is loaded:

enter image description here

Solution 1

Wait until the second redirect and then, get the url with the classic methods.

The web is developed on react, that's why is rendered with javascript. This complicate a little bit the selenium automation.

You need to search an element which presence indicate you that web is ready after the second redirect. I found this:

enter image description here

document.querySelector('[aria-label="sendo logo"]');

Translate this querySelector to selenium:

By.cssSelector("a[aria-label='sendo logo']");

And wait for it:

wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[aria-label='sendo logo']")));

If after this element, you keep getting the same url instead the full url, just try with another html element.

Solution 2

Try to get the headers of the page using selenium

JRichardsz
  • 14,356
  • 6
  • 59
  • 94