Github 搜索栏元素不可交互问题

问题描述 投票:0回答:1

我正在尝试使用 selenium 并编写一个简单的代码,它将打开浏览器并转到 github 网站。打开页面后,它将在搜索栏中搜索给定的关键字,但问题是,当我运行代码时,它显示该元素不可交互。发生这种情况是因为我可能由于java脚本而无法找到搜索栏元素code

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
url = "http://github.com"
driver.get(url)
searchInput = driver.find_element_by_xpath("//*[@id='query-builder-test']")
time.sleep(1)
searchInput.send_keys("python")
time.sleep(2)
searchInput.send_keys(Keys.ENTER)

我尝试使用javascript执行器来选择输入元素以及selenium查找元素方法,但无法搜索关键字see here

element=driver.execute_script("return document.getElementById('query-builder-test').value='python';")
searchInput = driver.find_element_by_xpath("//*[@id='query-builder-test']")
python selenium-webdriver css-selectors webautomation
1个回答
0
投票

据我所知,GitHub 搜索栏是一个按钮元素。我建议:

Button_text = ["Type"]
all_buttons = driver.find_elements(By.TAG_NAME, "button")#this will get all the buttons on the page for you. 

for button in all_buttons: #this looks for the buttons in the buttons library
                if button.text.lower() in Button_text: 
                                search_Button = button
                                search_Button.click() 
                                break
#this will loop through each button and look for the one that has the text type in it and set that as the button then click. it should open the search bar for you and 

我建议学习一些 HTML,因为它使 Selenium 的使用变得更容易 100 倍

这段代码并不完美,但它应该可以帮助您获得所需的结果,因为我现在正在工作,所以我无法对其进行太多研究,但我可以测试它并在回家时提供帮助。

© www.soinside.com 2019 - 2024. All rights reserved.