我正在制作一个机器人,它可以查看我的 LinkedIn 连接并打印它们。机器人进入我的 LinkedIn 联系人页面后,它会向下滚动以找到所有联系人,但一段时间后它会卡住,因为它无法找到并单击“显示更多结果”按钮。问题是 xpath id 每次都会改变,所以它应该通过阅读文本找到正确的按钮。
如何找到并点击它?
这是我的代码:
login_linkedin()
driver.get("https://www.linkedin.com/mynetwork/invite-connect/connections/")
# Get the current number of elements
elements = get_elements()
count=len(elements)
print("There are ",count," connections")
# Scroll down until no more elements are loaded
while True:
# Execute JavaScript code to scroll down
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait for new elements or button to appear
wait.until(lambda driver: len(driver.find_elements(By.CLASS_NAME, "mn-connection-card__name")) > count
)
# Click on the button if it exists
try:
print("I'm trying to find the Show more results button")
button = driver.find_elements(By.XPATH,"//*[contains(text(),'Show more results')]")
#button = driver.find_element(By.XPATH, show_more_button)
print("The show more results button is present")
button.click()
print("I clicked on Show more results")
except:
pass
# Update the current number of elements
old=count
elements = get_elements()
count=len(elements)
print("There are ",count," connections")
# Break if there are no more elements
if count == old:
break
# Loop through each element and print its text
for element in elements:
# Use BeautifulSoup to parse the HTML and get only the text
soup = BeautifulSoup(element.get_attribute("innerHTML"), "html.parser")
name = soup.get_text().strip()
print(name)
附上我要点击的元素的部分截图
我试图通过 ID 查找 XPATH 元素,但它总是在变化。我试图寻找文本“显示更多结果”,但它看起来不起作用。我尝试使用 LinkedIn API,但这也不起作用 - 可能是 VPN 问题,因为我在中国