2

I have this so far:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('C:\Users\Fan\Desktop\chromedriver.exe')
url = driver.current_url
print url

It keeps saying that line 4 "driver" is an invalid syntax. How would I fix this?

Also is there a way I can get all the current tabs open, and not just a single one?

EDIT: the above code works now; But I have another problem!

The code now opens a new tab, and for some reason the URL bar has "data;" in it, and it outputs data; as the print.

But I want it to take the existing URL from existing web browser already opened, how do I solve this?

1
  • Why do you want selenium to start working on an already open tab? That seems that you want your automated testing to require an action from you prior to running the test. It is best to automate all parts of the test. If you want the test to navigate to a specific page you can use Selenium to open that page. Commented Nov 22, 2015 at 0:26

2 Answers 2

4

In Python you do not specify the type of variable as is required in Java which is the reason for the error. The same error will also happen because your last line starts with String.

Calling webdriver.Chrome() returns a driver object so the line webdriver driver = new webdriver() is actually not needed.

The new keyword is not used in Python to create a new object.

Try this:

from selenium import webdriver

driver = webdriver.Chrome()
url = driver.getCurrentUrl()
Sign up to request clarification or add additional context in comments.

4 Comments

Hello, thanks for the solution! One problem I'm having now is after i execute it. It opens up a new window and inside the address bar it has "data;" But i want it to just take it from my current opened web browser
Selenium always starts in a new session instead of your currently running session. You are seeing data; because you didn't tell Selenium which page to open. getCurrentUrl is intended to retrieve the url when you don't know what it is, for example, after a redirect.
'WebDriver' object has no attribute 'getCurrentUrl'
the solution is driver.current_url
3

In order to extract the url of the current page from the web driver you have to call the current_url attribute:

from selenium import webdriver
import time

driver = webdriver.Chrome()

#Opens a known doi url
driver.get("https://doi.org/10.1002/rsa.1006")

#Gives the browser a few seconds to process the redirect
time.sleep(3)

#Retrieves the url after the redirect
#In this case https://onlinelibrary.wiley.com/doi/abs/10.1002/rsa.1006
url = driver.current_url 

2 Comments

What if I want to get the complete URL after redirect?
@bhattraideb the current_url attribute should return the entire URL... perhaps you need to give it a few more seconds to redirect... some website can take longer than 3 seconds.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.