这是在 GUI 中单击按钮后应运行的函数:
def button_event(self):
drug1 = self.entry1.get()
drug2 = self.entry2.get()
chrome_driver_path = "Chrome driver path "
# Set Chrome options to run in headless mode
chrome_options = Options()
# Create a new Chrome browser instance with the headless option
browser = webdriver.Chrome(executable_path=chrome_driver_path, options=chrome_options)
# Load the web page
browser.get('https://go.drugbank.com/drug-interaction-checker')
# Find the form element by ID
form = browser.find_element(By.CSS_SELECTOR,'form.name-search-form')
# Find the input fields and fill them out
name_input = form.find_element(By.CSS_SELECTOR,'input.select2-search__field')
name_input.send_keys(drug1)
time.sleep(2)
name_input.send_keys(Keys.RETURN)
name_input.send_keys(drug2)
time.sleep(2)
name_input.send_keys(Keys.RETURN)
time.sleep(2)
button = form.find_element(By.ID,"search")
time.sleep(4)
if button.is_enabled():
button.click()
print("button is enabled")
else :
description = "Unfound medics in database"
self.text1.delete("1.0","end")
self.text1.insert("0.0", description)
return
wait = WebDriverWait(browser, 20)
new_page_loaded = wait.until(EC.url_contains('drug-interaction-checker#results'))
time.sleep(2)
try:
# Find the element
browser.find_element(By.CSS_SELECTOR,"h2.no-results")
description = "No interaction found"
self.text1.delete("1.0","end")
self.text1.insert("0.0", description)
return
except NoSuchElementException:
print("results are there")
# Find the interactions div and extract the description text
interactions_div = browser.find_element(By.CSS_SELECTOR,'div.interactions-col.description')
description = interactions_div.find_element(By.TAG_NAME,'p').text
self.text1.delete("1.0","end")
self.text1.insert("0.0", description)
这应该打开
drugBank
网站并输入两个药物,然后单击该按钮检查交互,不知怎的,这不起作用,因为即使单击该按钮,即使手动也不会发生任何事情,但是当我自己进入该网站时,一切都发生了向右走。
抱歉,我的业力太低,无法发表评论,但我觉得这可能很有用。目前,当我使用 Firefox Web 浏览器手动单击该按钮时,没有任何反应。您的问题可能是由于手动测试与自动测试时浏览器/chrome 版本不同所致。
我将按钮单击替换为:
browser.execute_script(
"document.getElementById('drug-interactions').submit();"
)
这样就不需要按钮了,一切都按预期工作。