为什么 selenium 找不到 Disqus 评论区?
Why selenium is unable to locate the Disqus comment section?
你好
我之前在 python 上创建了几个 selenium 脚本,它们运行良好,但不知道为什么它不能在网站上运行。
我正在尝试单击在该网站上加载 "DISQUS" 时出现的元素。
Disqus 是网站上的评论部分,在网站加载后加载。
所以我使用了 sleep 方法,但它没有用,所以我尝试用 python IDLE 来逐行执行代码,这样我就可以 运行 定位代码一次 Disqus 评论部分 完全可用,但仍然出现相同的错误。 "Webdriver unable to locate element"
这是我的代码。
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
browser = webdriver.Chrome('E:\ChromeDriver\chromedriver_win32\chromedriver.exe')
browser.get('https://www.eloanus.com/customers-review')
dicuss = browser.find_element_by_class_name('textarea')
print(dicuss)
错误,我正在处理。
Traceback (most recent call last):
File "C:\Users\Acer-573G\PycharmProjects\Appium\venv\import selenium.py", line 12, in <module>
dicuss = browser.find_element_by_class_name('textarea')
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 563, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
'value': value})['value']
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"textarea"}
(Session info: chrome=69.0.3497.100)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)
我附上了那个网站的 inspect element (class) 的屏幕截图。
Inspect element screen shot
我是编程新手,如有任何帮助,我们将不胜感激。
有多个 iframe,因此您必须切换到它才能进行交互。
完成后将驱动程序的控制切换为默认设置始终是一个好习惯。
代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
driver.maximize_window()
driver.get("https://www.eloanus.com/customers-review")
wait = WebDriverWait(driver, 20)
visibilty_login = wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Log In']")))
action = ActionChains(driver)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id$='disqusCommentsHolder']")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='Disqus']")))
driver.execute_script("window.scrollTo(0, 100)")
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Join the discussion…']/following-sibling::div[@class='textarea']"))).send_keys("Hi")
driver.switch_to.default_content()
希望对您有所帮助。
要将 字符序列 发送到 Disqus 评论部分,您需要:
- 为所需的父 框架引入 WebDriverWait 并切换到它
- 诱导 WebDriverWait 以获得所需的嵌套 框架并切换到它
- 诱导 WebDriverWait 使所需的 元素可点击,您可以使用以下解决方案:
代码块:
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')
browser=webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
browser.get('https://www.eloanus.com/customers-review')
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='disq2disqusCommentsHolder']")))
WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@id,'dsq-app') and @title='Disqus']")))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='textarea'][contains(@aria-label,'Join the discussion')]"))).send_keys("Michael Berger")
浏览器快照:
你好
我之前在 python 上创建了几个 selenium 脚本,它们运行良好,但不知道为什么它不能在网站上运行。 我正在尝试单击在该网站上加载 "DISQUS" 时出现的元素。 Disqus 是网站上的评论部分,在网站加载后加载。
所以我使用了 sleep 方法,但它没有用,所以我尝试用 python IDLE 来逐行执行代码,这样我就可以 运行 定位代码一次 Disqus 评论部分 完全可用,但仍然出现相同的错误。 "Webdriver unable to locate element"
这是我的代码。
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
browser = webdriver.Chrome('E:\ChromeDriver\chromedriver_win32\chromedriver.exe')
browser.get('https://www.eloanus.com/customers-review')
dicuss = browser.find_element_by_class_name('textarea')
print(dicuss)
错误,我正在处理。
Traceback (most recent call last):
File "C:\Users\Acer-573G\PycharmProjects\Appium\venv\import selenium.py", line 12, in <module>
dicuss = browser.find_element_by_class_name('textarea')
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 563, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
'value': value})['value']
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\python3.5\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"textarea"}
(Session info: chrome=69.0.3497.100)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.17134 x86_64)
我附上了那个网站的 inspect element (class) 的屏幕截图。
Inspect element screen shot
我是编程新手,如有任何帮助,我们将不胜感激。
有多个 iframe,因此您必须切换到它才能进行交互。
完成后将驱动程序的控制切换为默认设置始终是一个好习惯。
代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path = r'D:/Automation/chromedriver.exe')
driver.maximize_window()
driver.get("https://www.eloanus.com/customers-review")
wait = WebDriverWait(driver, 20)
visibilty_login = wait.until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Log In']")))
action = ActionChains(driver)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id$='disqusCommentsHolder']")))
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='Disqus']")))
driver.execute_script("window.scrollTo(0, 100)")
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Join the discussion…']/following-sibling::div[@class='textarea']"))).send_keys("Hi")
driver.switch_to.default_content()
希望对您有所帮助。
要将 字符序列 发送到 Disqus 评论部分,您需要:
- 为所需的父 框架引入 WebDriverWait 并切换到它
- 诱导 WebDriverWait 以获得所需的嵌套 框架并切换到它
- 诱导 WebDriverWait 使所需的 元素可点击,您可以使用以下解决方案:
代码块:
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') browser=webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe') browser.get('https://www.eloanus.com/customers-review') WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='disq2disqusCommentsHolder']"))) WebDriverWait(browser, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@id,'dsq-app') and @title='Disqus']"))) WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='textarea'][contains(@aria-label,'Join the discussion')]"))).send_keys("Michael Berger")
浏览器快照: