'How do I iterate and visit each link in a list using selenium and python? How do I click on each link on a Yelp page
I would love to visit each link on this yelp page that has Dumpling included in the link text.
Picture of List of Links on yelp
Here is the code. I got to the second page but not sure on how to click into each link. I want each link to open in a new. but only dumpling ones. Thank you. Hope this makes sense.
all_links = []
#yelp page
driver.get("https://yelp.com");
#Selecting the first box
type_of_food = driver.find_element_by_id('find_desc')
#Enter Chinese Food
type_of_food.send_keys('Chinese Food');
#press enter
type_of_food.send_keys(Keys.ENTER);
driver.implicitly_wait(30)
#Button click
driver.find_element_by_id("header-search-submit").click()
driver.implicitly_wait(9)
driver.find_element_by_partial_link_text('Dumpling')
for link in all_links:
links.append(link.get_attribute('href'))
Solution 1:[1]
I am not sure if your code is all correct.
A. You have typed Chinese Food
in the search area, then keying in ENTER
, then again clicking on Search
button. But only one of them would do. Both doesn't seem to be required.
B. for link in all_links:
I do not understand why you are trying to loop through a declared blank list (list declared on top: all_links = []
). It does not have anything in it. Assuming that you are trying to link through the dumpling elements, I have refactored your code to below:
all_links = []
#yelp page
driver.get("https://yelp.com")
#Selecting the first box
type_of_food = driver.find_element(By.ID, 'find_desc')
#Enter Chinese Food
type_of_food.send_keys('Chinese Food')
#press enter
# type_of_food.send_keys(Keys.ENTER)
driver.implicitly_wait(30)
#Button click
driver.find_element(By.ID, "header-search-submit").click()
driver.implicitly_wait(9)
dumplings = driver.find_elements(By.PARTIAL_LINK_TEXT, 'Dumpling')
for item in dumplings:
all_links.append(item.get_attribute('href'))
# Replace CONTROL with COMMAND if you are on Mac
ActionChains(driver).key_down(Keys.CONTROL).click(item).key_up(Keys.CONTROL).perform()
print(all_links)
Output:
['https://www.yelp.com/biz/dumpling-kitchen-san-francisco?osq=Chinese+Food', 'https://www.yelp.com/biz/dumpling-house-san-francisco?osq=Chinese+Food', 'https://www.yelp.com/biz/dumpling-baby-china-bistro-san-francisco-4?osq=Chinese+Food', 'https://www.yelp.com/search?find_desc=Dumplings&find_loc=San+Francisco%2C+CA']
Process finished with exit code 0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |