Afternoon all. Currently trying to use Selenium webdriver to loop through a list of links on a page. Specifically, it\'s clicking a link, grabbing a line of text off said pa
I'm not sure if this will fix the problem, but in general it is better to use WebDriverWait
rather than implicitly_wait
since WebDriveWait.until will keep calling the supplied function (e.g. driver.find_element_by_xpath
) until the returned value is not False
-ish or the timeout (e.g 5000 seconds) is reached -- at which point it raises a selenium.common.execptions.TimeoutException
.
import selenium.webdriver.support.ui as UI
def test_text_saver(self):
driver = self.driver
wait = UI.WebDriverWait(driver, 5000)
with open("textsave.txt","w") as textsave:
list_of_links = driver.find_elements_by_xpath("//*[@id=\"learn-sub\"]/div[4]/div/div/div/div[1]/div[2]/div/div/ul/li/a")
for link in list_of_links: # 2
link.click() # 1
text = wait.until(
lambda driver: driver.find_element_by_xpath("//*[@id=\"learn-sub\"]/div[4]/div/div/div/div[1]/div[1]/div[1]/h1").text)
textsave.write(text+"\n\n")
driver.back()
wait.until
is placed directly after link.click()
Instead of using
while x <= link_count:
...
x += 1
it is better to use
for link in list_of_links:
For one think, it improves readability. Moreover, you really don't
need to care about the number x
, all you really care about is
looping over the links, which is what the for-loop
does.