OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to replace the url in a loop using Selenium Python

  • Thread starter Thread starter 4non
  • Start date Start date
4

4non

Guest
Just like the title says, how do I write the code in python if I want to replace a part of the URL.

For this example replacing a specific part by 1, 2, 3, 4 and so on for this link (https://test.com/page/1), then doing something on said page and going to the next and repeat.

So, "open url > click on button or whatever > replace link by the new link with the next number in order"

(I know my code is a mess I am still a newbie, but I am trying to learn and I am adding whatever mess I've wrote so far to follow the posting rules)

Code:
PATH = Service("C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=PATH)
driver.maximize_window()

get = 1
url = "https://test.com/page/{get}"

while get < 5:
    driver.get(url)
    time.sleep(1)
    driver.find_element_by_xpath("/html/body/div/div/div[2]/form/section[3]/input[4]").click()
    get = get + 1
    driver.get(url)
    driver.close()
<p>Just like the title says, how do I write the code in python if I want to replace a part of the URL.</p>
<p>For this example replacing a specific part by 1, 2, 3, 4 and so on for this link (<a href="https://test.com/page/1" rel="nofollow noreferrer">https://test.com/page/1</a>), then doing something on said page and going to the next and repeat.</p>
<p>So, "open url > click on button or whatever > replace link by the new link with the next number in order"</p>
<p>(I know my code is a mess I am still a newbie, but I am trying to learn and I am adding whatever mess I've wrote so far to follow the posting rules)</p>
<pre><code>PATH = Service("C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=PATH)
driver.maximize_window()

get = 1
url = "https://test.com/page/{get}"

while get < 5:
driver.get(url)
time.sleep(1)
driver.find_element_by_xpath("/html/body/div/div/div[2]/form/section[3]/input[4]").click()
get = get + 1
driver.get(url)
driver.close()
</code></pre>
 

Latest posts

M
Replies
0
Views
1
Mohit Pant
M
Top