我知道有关此问题有很多,但似乎没有帮助。使用键打开新的选项卡或窗口,以使元素不会过时,则不会执行任何操作。不会打开新标签。它什么也没做。 我正在抓取一个网站,该网站的主页包含一个链接列表,我需要通过单击每个链接来循环并从每个链接获取数据。在第一次迭代之后,当返回到主页时,现在显然已经过时了。这是我第一次使用Selenium,所以我为这个幼稚的问题表示歉意,但是我已经被困了好几天了,在这一点上我非常渴望获得帮助。这是我的原始代码: 从硒导入webdriver
#Main url with a list on links of doctors name
url = ('https://weedmaps.com/doctors/in/massachusetts')
chromedriver = 'C:/Users/Henry/Desktop/chromedriver'
driver = webdriver.Chrome(chromedriver)
#Get the URL
driver.get(url)
#The element list
elements = driver.find_elements_by_class_name('details')
#Data I need
name = []
number = []
address = []
email = []
website = []
#Loop through elements
for i in elements:
#clicks the element
i.click()
#New element once first element is clicked
popup = driver.find_element_by_class_name('no-decoration')
#Click the new element that opens a new window with the data I need
popup.click()
driver.implicitly_wait(10)
#Data I need
anchors = driver.find_elements_by_tag_name('a')
address.append(anchors[29].text)
number.append(anchors[30].text)
email.append(anchors[31].text)
website.append(anchors[35].text)
#Go back to the list and go to the next element
driver.back()
答案 0 :(得分:0)
您正在执行更新DOM的操作,因此elements
在第一次迭代时有效就不再有效(变成过时)...
代替
elements = driver.find_elements_by_class_name('details')
for i in elements:
i.click()
尝试
elements_len = len(driver.find_elements_by_class_name('details'))
for i in range(elements_len):
driver.find_elements_by_class_name('details')[i].click()