Reputation: 7
I'm working on this site drugs.com to see interaction between two drugs . i added two drug name in search box of this site and i need to click on check interactions box in this site to see interaction between two drugs in another window .
I used several methods to solve this problem, but I could not solve this problem. This is my code that add two drugs in search box:
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# Launch the Chrome browser
driver = webdriver.Chrome()
# Navigate to the drugs.com drug interactions page
driver.get("https://www.drugs.com/drug_interactions.html")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#livesearch-interaction-basic"))).send_keys('aspirin' +Keys.RETURN)
search_box = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#livesearch-interaction")))
search_box.clear()
search_box.send_keys('Aleve')
search_box.send_keys(Keys.RETURN)
HTML snapshot:
Upvotes: -1
Views: 103
Reputation: 15461
Here's a complete SeleniumBase script that automates that.
First pip install seleniumbase
, then run with python
:
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__, "--slow", "--ad-block")
class MyTest(BaseCase):
def test_drug_interactions(self):
self.open("https://www.drugs.com/drug_interactions.html")
self.type("input#livesearch-interaction-basic", "Aspirin")
self.click('a:contains("Aspirin")')
self.type("input#livesearch-interaction", "Aleve")
self.click('a:contains("Aleve")')
self.click('a[href*="/interactions-check.php?drug_list"]')
self.sleep(5)
Upvotes: 0
Reputation: 193078
To click on the element with text as "Check Interactions" you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Check Interactions"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href^='/interactions-check']"))).click()
Using XPATH:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Check Interactions')]"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Upvotes: 1