Hello I wrote this selenium code to click Next button and give me url of the next page.
The Question is
I want to convert this code in a loop so I can click on the next button & collect all URLs until next button disappears.
How do I out all the collected URLs in a list?
JavaScript
x
6
1
next = driver.find_element(By.LINK_TEXT, "Next")
2
next.click()
3
urrl = driver.current_url
4
print(urrl)
5
driver.quit()
6
I tried While True loop for this.
JavaScript
1
8
1
while True:
2
try:
3
urrl = driver.current_url **## I tried this line after clicking the next button as well**
4
next = driver.find_element(By.LINK_TEXT,"Next")
5
next.click()
6
except:
7
break
8
I was able to click on the next button until the end but I can not figure out how to collect url of the webpage and how to append them into a list.
Tried append but I think I am doing something wrong.
Advertisement
Answer
You can write a function to test if the element exists:
JavaScript
1
12
12
1
def is_element_exists(xpath, id_flag=False):
2
try:
3
if id_flag:
4
driver.find_element_by_id(xpath)
5
else:
6
driver.find_element_by_xpath(xpath)
7
return True
8
except Exception as e:
9
# print("Excpetion:[%s][%s]" % (e, traceback.format_exc()))
10
print('do not find the node')
11
return False
12