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

只谈情不闲聊 提交于 2019-12-11 15:16:01

问题


I can't find the element I need to tell selenium that I want it to click it, I believe it is because the page is generated by javascript

can someone please help? maybe show me a way to do it and then explain how to find?

the website I'm working on is www.howlongtobeat.com

I want selenium to do the following:

go to http://www.howlongtobeat.com => click on the search tab => enter "God of War (2018)" => click the link that pops up

this is the code I have so far:

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()

it's just the last step I need help with

can someone advise?


回答1:


@Ankur Singh solution works fine. You can also use the CSS Selector to do the same clicking (I generally prefer CSS Selectors)

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()



回答2:


You can use below code to click on 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()


来源:https://stackoverflow.com/questions/50892591/cant-find-the-element-i-need-in-javascript-with-selenium-to-click-on-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!