-1

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:

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Can you give us a feedback to the [answer](https://stackoverflow.com/a/76510196/7429447) published against your previous [question](https://stackoverflow.com/q/76510129/7429447)? You seem to be using the code solution from the answer. – undetected Selenium Jun 20 '23 at 18:29
  • @undetectedSelenium thank you for help me but i can't vote for your answer because he wants me to have 15 reputation so that I can't vote so I'm sorry – Abdelrhman Samir Jun 20 '23 at 18:42
  • 1
    [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – undetected Selenium Jun 20 '23 at 18:43
  • @undetectedSelenium I have tried it, but there is a problem that it clicks on the check interactions box after adding or entering the name of the first drug and completely ignores adding or entering the name of the second drug (I want to add or enter the name of the two drugs and then press the check box until I go to the interaction check interactions page and Extract some information from it.) – Abdelrhman Samir Jun 20 '23 at 19:20
  • Did you go through my previous comment/link? – undetected Selenium Jun 20 '23 at 19:22

2 Answers2

1

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I have tried it, but there is a problem that it clicks on the check interactions box after adding or entering the name of the first drug and completely ignores adding or entering the name of the second drug (I want to add or enter the name of the two drugs and then press the check box until I go to the interaction check interactions page and Extract some information from it.) @undetected Selenium – Abdelrhman Samir Jun 20 '23 at 19:19
0

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)
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48