Javascript 无法使用Selenium单击按钮
据 ,我试图从pantip.com获取数据,包括所有评论和每个评论的回复 我在获取每条评论的回复文本时遇到问题。我使用selenium单击一个按钮以获取其中的文本。但是,只有当我将页面滚动到按钮的位置时,它才起作用 这里是一个错误,如果我不滚动Javascript 无法使用Selenium单击按钮,javascript,python,selenium,selenium-webdriver,beautifulsoup,Javascript,Python,Selenium,Selenium Webdriver,Beautifulsoup,据 ,我试图从pantip.com获取数据,包括所有评论和每个评论的回复 我在获取每条评论的回复文本时遇到问题。我使用selenium单击一个按钮以获取其中的文本。但是,只有当我将页面滚动到按钮的位置时,它才起作用 这里是一个错误,如果我不滚动 WebDriverException: unknown error: Element <a href="javascript:void(0)" class="reply see-more">...</a> is not clic
WebDriverException: unknown error: Element <a href="javascript:void(0)" class="reply see-more">...</a> is not clickable at point (518, 507). Other element would receive the click: <select class="dropdown-jump">...</select>
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.17134 x86_64)
selenium可以使用
执行脚本(“xxxx”)
执行脚本,例如:
script = """function getComments(){var comments=new Array();a = $('div.display-post-story');for (var i=0;i<a.length;i++){comments.push(a[i].innerText)};return comments;}"""
comments = driver.execute_script(script)
# then u can deal with all comments.
script=”“”函数getComments(){var comments=new Array();a=$('div.display-post-story');for(var i=0;iselenium可以使用execute_script(“xxxx”)
执行脚本,例如:
script = """function getComments(){var comments=new Array();a = $('div.display-post-story');for (var i=0;i<a.length;i++){comments.push(a[i].innerText)};return comments;}"""
comments = driver.execute_script(script)
# then u can deal with all comments.
script=”“”函数getComments(){var comments=new Array();a=$('div.display-post-story');for(var i=0;i页面底部有一个固定的导航面板,所以当您试图单击按钮时,实际上是从该面板中单击了元素,这就是引发异常的原因……您可能需要
- 滚动至所需按钮
- 向下滚动一点
- 单击按钮查看回复
from selenium.webdriver.common.keys import Keys
for reply in driver.find_elements_by_xpath('//div[starts-with(@id, "reply-comment-")]/a'):
driver.execute_script('arguments[0].scrollIntoView();', reply)
reply.send_keys(Keys.DOWN)
reply.click()
页面底部有一个固定的导航面板,因此当您试图单击按钮时,实际上是在该面板中单击元素,这就是引发异常的原因…您可能需要
- 滚动至所需按钮
- 向下滚动一点
- 单击按钮查看回复
from selenium.webdriver.common.keys import Keys
for reply in driver.find_elements_by_xpath('//div[starts-with(@id, "reply-comment-")]/a'):
driver.execute_script('arguments[0].scrollIntoView();', reply)
reply.send_keys(Keys.DOWN)
reply.click()
单击每个评论按钮并提取每个评论的回复文本,您不需要单独使用Beautiful Soup和Selenium。要实现这一点,您必须将所需元素滚动到中,并且可以使用以下解决方案:
- 代码块:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver= webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://pantip.com/topic/38372443")
comment_buttons = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.reply.see-more span.focus-txt")))
for button in comment_buttons:
driver.execute_script("return arguments[0].scrollIntoView(true);", button)
button.click()
print("Comment button clicked")
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class^='comment'][data-refcm^='comment'] div.display-post-story>a"))).get_attribute("innerHTML"))
driver.quit()
- 控制台输出:
Comment button clicked
Comment button clicked
Comment button clicked
Comment button clicked
https://www.instagram.com/clintbondad
.
.
.
单击每个评论按钮并提取每个评论的回复文本,您不需要单独使用Beautiful Soup和Selenium。要实现这一点,您必须将所需元素滚动到中,并且可以使用以下解决方案:
- 代码块:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver= webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://pantip.com/topic/38372443")
comment_buttons = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.reply.see-more span.focus-txt")))
for button in comment_buttons:
driver.execute_script("return arguments[0].scrollIntoView(true);", button)
button.click()
print("Comment button clicked")
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class^='comment'][data-refcm^='comment'] div.display-post-story>a"))).get_attribute("innerHTML"))
driver.quit()
- 控制台输出:
Comment button clicked
Comment button clicked
Comment button clicked
Comment button clicked
https://www.instagram.com/clintbondad
.
.
.
根据您的异常详细信息,您可以使用action类单击webelement,在这里您可以根据您的异常详细信息,您可以使用action类单击webelement,在这里您可以单击,谢谢。它可以工作,但为什么我们必须“向下滚动一点”。@t.WandeeWandee,这是因为当我们执行scrollIntoView()时
required链接仍然不可点击,因为它被固定的底部面板覆盖。因此,需要再按一次向下键,使其完全可见/可点击谢谢。这是可行的,但为什么我们必须“向下滚动一点”。@t.WandeeWandee,这是因为当我们这样做时,scrollIntoView()
所需链接仍然不可单击,因为它被固定的底部面板覆盖。因此,需要再按一次向下键才能使其完全可见/可单击谢谢。它可以工作。您能向我解释一下comment\u buttons=WebDriverWait(driver,20)是如何工作的吗。直到……工作。@t.WandeeWandee这行代码comment\u buttons=WebDriverWait(driver,20)。直到(EC.visibility\u所有元素的位置((By.CSS\u SELECTOR,“a.reply.see-more span.focus txt”))
收集标识为comment按钮的元素并存储在列表中,以便我们可以调用click()
方法。谢谢。它可以工作。你能给我解释一下注释按钮=WebDriverWait(driver,20)。直到……工作。@t.WandeeWandee这行代码注释按钮=WebDriverWait(driver,20)。直到(EC.所有元素的可见性(By.CSS\u选择器,“a.reply.see-more span.focus txt”))
收集标识为comment button的元素并存储在列表中,以便我们可以通过迭代对其调用click()
方法。