2

I have a file full of Urls (about some 3000 urls) and the requirement is to get each url invoked in a browser tab and click on each page (just the click is enough on the loaded page), which redirects to a different page. This is all I need to do.

I have written a code for the urls to retrieve one by one and clink on the page (page is designed to click anywhere to land or redirect to a different page). However, while running the code an error is being thrown with "invalid argument". Any thoughts, how can I correct the error ?

Appreciate your support.

 with open(dataFile, 'r') as urlFile:
 urlFile.readline()
 for url in urlFile:
 driver = webdriver.Chrome('C:\\chromedriver_win32\\chromedriver.exe')
 driver.get(url)
 link = driver.find_element_by_xpath("/html/body")
 link.click()
 urlFile.close()

The above code should pull each url from the file on a new browser tab and click. Repeat the same until all the urls in the file are done.

asked Sep 10, 2019 at 4:37
5
  • 2
    which line you are getting the error? and is there any blank lines in the text file. also, check the URL format and it should be starting link http:// or https:// Commented Sep 10, 2019 at 5:15
  • 1
    @Murthi, you are correct. I have the /r (carriage return) in my code and I have it removed and it is working good. Thank you for pointing this out. Commented Sep 10, 2019 at 5:38
  • 1
    @Murthi, I have another problem with this code. Let's say, I have n lines of Urls in the file - however, the code executes n-1 and leaves the last Urls from the file. Any suggestions, please ? Commented Sep 10, 2019 at 5:42
  • 2
    remove the code urlFile.readline() in the loop(second line in your code). then it will execute all the lines. Commented Sep 10, 2019 at 8:47
  • 1
    Thank you very much. It works brilliantly. Commented Sep 10, 2019 at 19:05

1 Answer 1

4

I did something like this Code

from selenium import webdriver
import time
driver = webdriver.Chrome('driver_location')
f = open("dataFile.txt", 'r')
for line in f:
 driver.get(line)
 link = driver.find_element_by_css_selector("#header > div:nth-child(3) > div > div > div:nth-child(3) > div > a")
 link.click()
 print(driver.current_url)
f.close()

url in dataFile.txt file

http://automationpractice.com/index.php
http://automationpractice.com/index.php
http://automationpractice.com/index.php

output

http://automationpractice.com/index.php?controller=order
http://automationpractice.com/index.php?controller=order
http://automationpractice.com/index.php?controller=order

Hope this helps.

answered Sep 10, 2019 at 5:22
Sign up to request clarification or add additional context in comments.

2 Comments

This worked well too. So, I have removed the /r from my code to output the urls to the file. It is working good. However, I have n lines of Urls in the file - however, the code executes n-1 and leaves the last Urls from the file. Any good suggestions, please ?
With example above it is executing all URLs. I tried with three as shown above.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.