我已经让selenium完美地工作但我想知道如何点击我输入的字符串的链接:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
url = "http://www.howlongtobeat.com"
driver = webdriver.Chrome()
driver.get(url)
search_element = driver.find_element_by_name("global_search_box")
search_element.clear()
search_element.send_keys("God of War (2018)")
search_element.send_keys(Keys.RETURN)
link = driver.find_element_by_xpath("//div id=[@class='nav_playthroughs_load]")
link.click()
如何点击标题为“战神(2018)”的链接
我收到了这个错误:
WebDriverException Traceback (most recent call last)
<ipython-input-7-0343b11486bd> in <module>()
----> 1 search_element = driver.find_element_by_name("global_search_box")
2 search_element.clear()
3 search_element.send_keys("God of War (2018)")
4 search_element.send_keys(Keys.RETURN)
5
~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in find_element_by_name(self, name)
485 element = driver.find_element_by_name('foo')
486 """
--> 487 return self.find_element(by=By.NAME, value=name)
488
489 def find_elements_by_name(self, name):
~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in find_element(self, by, value)
953 return self.execute(Command.FIND_ELEMENT, {
954 'using': by,
--> 955 'value': value})['value']
956
957 def find_elements(self, by=By.ID, value=None):
~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
310 response = self.command_executor.execute(driver_command, params)
311 if response:
--> 312 self.error_handler.check_response(response)
313 response['value'] = self._unwrap_value(
314 response.get('value', None))
~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
WebDriverException: Message: chrome not reachable
(Session info: chrome=67.0.3396.79)
(Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.15.0-23-generic x86_64)
感谢,
答案 0 :(得分:0)
要点击带有战争之神(2018)的文字的链接,您必须诱导 WebDriverWait 以使元素可点击并且您可以使用以下任一解决方案:
LINK-TEXT :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "God of War (2018)"))).click()
CSS-SELECTOR :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.text_blue[title^='God of War']"))).click()
XPATH :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='text_blue'][@title=\"God of War (2018)\"]"))).click()