在 Javascript 中找不到我需要的元素,用 selenium 点击 link

can't find the element I need in Javascript with selenium to click on link

我找不到我需要告诉selenium我想让它点击它的元素,我相信这是因为页面是由javascript

生成的

有人可以帮忙吗?也许告诉我一种方法,然后解释如何找到?

我正在处理的网站是 www.howlongtobeat.com

我希望 selenium 执行以下操作:

转到 http://www.howlongtobeat.com => 单击搜索选项卡 => 输入 "God of War (2018)" => 单击弹出的 link

这是我目前的代码:

from selenium import webdriver  
from selenium.common.exceptions import NoSuchElementException  
from selenium.webdriver.common.keys import Keys 
from requests import get
from bs4 import BeautifulSoup

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)


#this is where my isssue is, I dont know what element it is or how to find
link = driver.find_element_by_link_text("input")
link.click()

这只是我需要帮助的最后一步

有人可以建议吗?

您可以使用以下代码点击link:-

element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, "search_list_image"))
link = driver.find_element_by_link_text("God of War (2018)")
link.click()

@Ankur Singh 解决方案效果很好。您也可以使用 CSS 选择器进行相同的点击(我通常更喜欢 CSS 选择器)

element=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR,"h3.shadow_text")))
element1= driver.find_element_by_css_selector('h3.shadow_text > a')
element1.click()
time.sleep(3)
driver.quit()