【发布时间】:2021-05-10 12:05:31
【问题描述】:
我只是想自动化一个像Replika(聊天机器人)这样的网站。在其中,一个新的聊天总是不断出现,但带有一个全新的 xpath 和 id。我越来越难以跟踪最近与 selenium 的聊天。我确实尝试了here 和here 列出的解决方案,但它们对我不起作用(或者我做错了什么)。我刚开始使用 selenium,所以我对它了解的不多。请帮帮我。我正在使用 python 3.8.2。
代码如下:
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from urllib.request import urlopen
browser = webdriver.Chrome("C:\Chromedriver\chromedriver.exe")
browser.get("https://my.replika.ai/")
time.sleep(3)
browser.find_element_by_xpath("""//*
[@id="root"]/div/div[1]/main/a[2]""").click()
time.sleep(2)
### Login ###
browser.find_element_by_xpath("""//*
[@id="emailOrPhone"]""").send_keys("gmail_id")
time.sleep(1)
browser.find_element_by_xpath("""//*[@id="loginForm"]/button""").click()
time.sleep(3)
### Password ###
browser.find_element_by_xpath("""//*[@id="login-
password"]""").send_keys("gmail_password")
time.sleep(1)
browser.find_element_by_xpath("""//*[@id="loginForm"]/button""").click()
time.sleep(10)
### Accept the cookies ###
browser.find_element_by_xpath("""//*
[@id="root"]/div/div[1]/div[1]/button""").click()
time.sleep(5)
### Getting the Latest text ### Here is where it doesn't work
# This is a implementation that I tried and it didn't work
url = "https://my.replika.ai/"
# We use try-except in case the request was unsuccessful because of
# wrong URL
try:
page = urlopen(url)
except Exception:
print("Error opening the URL")
soup = BeautifulSoup(page, 'html.parser')
content = soup.find('div', {"id": "chat-messages"})
chat = ''
for i in content.findAll('span'):
chat = chat + ' ' + i.text
print(chat)
提前致谢。
【问题讨论】:
-
so I don't know a lot of things about it.所以没有人知道你的代码和你的试验。请查看tour 和minimal reproducible example 并再次编辑您的帖子。 -
抱歉错误,帖子已更新。
标签: python selenium xpath webdriver element