尝试在网站中查找ID并出现“无法访问Chrome”错误

时间:2018-09-27 14:46:08

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver

这是我的代码。

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from bs4 import BeautifulSoup
import time
from time import sleep
browser = webdriver.Chrome("C:/Utility/chromedriver.exe")

wait = WebDriverWait(browser, 5)

web = Browser()
url = 'https://www_corp_site/admin/?page=0'
web.go_to(url)

web.type('first.last@gmail.com', into='username')
web.click('Next')
# Wait for 2 seconds
time.sleep(1)

# password
web.type('pswd', into='Password')
web.click('Next')
time.sleep(1)

element = browser.find_element_by_id('main_content')
print(element.text)

由于某些原因,两个Chrome浏览器正在打开,并且出现此错误:

WebDriverException: chrome not reachable
  (Session info: chrome=69.0.3497.100)
  (Driver info: chromedriver=2.39.562718 (9a2698cba08cf5a471a29d30c8b3e12becabb0e9),platform=Windows NT 10.0.17134 x86_64)

如何打开一个浏览器,引用该浏览器,并打印“ main_content” ID中的所有数据?还是表ID ='dags'?

enter image description here

1 个答案:

答案 0 :(得分:4)

您正在呼叫webdriver.Chrome()Browser()。我不知道Browser()是什么,但它似乎正在打开另一个chrome实例。因此,当您定义变量browser和变量web时,您将分别获得它们的Chrome实例。试试这个:

browser = webdriver.Chrome("C:/Utility/chromedriver.exe")

wait = WebDriverWait(browser, 5)

url = 'https://www_corp_site/admin/?page=0'
browser.get(url)

browser.find_element_by_id('username').type('first.last@gmail.com')
browser.find_element_by_id('Next').click()
# Instead of sleeping, use Selenium's wait feature to proceed as soon as the element is available
WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.ID, 'Password')))

# password
browser.find_element_by_id('Password').type('pswd')
browser.find_element_by_id('Next').click()
WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.ID, 'main_content')))

element = browser.find_element_by_id('main_content')
print(element.text)

我不确定这些ID是否是元素的ID,因此您可能必须更改这些ID。