A Bedoya
A Bedoya

Reputation: 85

Selenium Python Unable to Locate Element in Dropdown Toolbar

I am trying to create a tool where I can input a chromosomal position and subsequently get its primers. I have the following code that works in locating the search bar to input my text, but I am not able to locate the "Tools" dropdown menu where the button/link for getting primers is.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.ncbi.nlm.nih.gov/genome/gdv/browser/genome/?id=GCF_000001405.39")


elem = driver.find_element_by_id("loc-search") #find search bar
elem.clear() #clear search bar
elem.send_keys("chr7:26,197,693-26,197,712") #enter guide
elem.send_keys(Keys.RETURN)

tools = driver.find_element_by_id("button-1075")
tools.send_keys(Keys.RETURN)

This gives me the following error

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="button-1075"]"}
  (Session info: chrome=80.0.3987.149)

This is what the html looks like

<a class="x-btn x-unselectable x-box-item x-toolbar-item x-btn-default-toolbar-small" 
hidefocus="on" unselectable="on" role="button" 
aria-hidden="false" aria-disabled="false" aria-label="Tools" 
aria-haspopup="true" id="button-1075" tabindex="0" 
data-qtip="Tools" data-componentid="button-1075" 
style="left: 431px; top: 0px; margin: 0px;"><span 
id="button-1075-btnWrap" data-ref="btnWrap" 
role="presentation" unselectable="on" style="" 
class="x-btn-wrap x-btn-wrap-default-toolbar-small 
x-btn-arrow x-btn-arrow-right"><span id="button-1075-btnEl" 
data-ref="btnEl" role="presentation" unselectable="on" style="" 
class="x-btn-button x-btn-button-default-toolbar-small 
x-btn-text  x-btn-icon x-btn-icon-left x-btn-button-center 
"><span id="button-1075-btnIconEl" data-ref="btnIconEl" 
role="presentation" unselectable="on" 
class="x-btn-icon-el x-btn-icon-el-default-toolbar-small xsv-tools " 
style=""></span><span id="button-1075-btnInnerEl" 
data-ref="btnInnerEl" unselectable="on" 
class="x-btn-inner x-btn-inner-default-toolbar-small">Tools</span></span></span></a>

And what the website looks like; I want to access the Tools dropdown/BLAST and Primer Search/Primer BLAST (Visible Range) enter image description here

Here are a variety of different ways I have tried to find this element

#tools.execute_script("arguments[0].setAttribute('aria-pressed',true);", tools)

#wait = WebDriverWait(driver, 10)
#wait.until(EC.element_to_be_clickable((By.ID, 'button-1075'))).click()
#driver.find_element_by_css_selector("BLAST and Primer Search")

#driver.find_element_by_css_selector("Primer BLAST (Visible Range)")
#send_keys(Keys.RETURN)

#driver.select_by_visible_text('BLAST and Primer Search')
#driver.select_by_visible_text("Primer BLAST (Visible Range)")

#tools = driver.find_element_by_class_name("x-btn x-unselectable x-box-item x-toolbar-item x-btn-default-toolbar-small x-focus x-btn-focus x-btn-default-toolbar-small-focus x-btn-menu-active x-btn-over")
#tools.select_by_visible_text('Blast')

#right = driver.find_element_by_id("menu-1386") #right click?
#right.send_keys(Keys.RETURN)

#wait.until(EC.visibility_of_element_located((By.ID,'button-1075'))).click()

#tools = driver.find_element_by_id("button-1075-btnEl")
#tools.send_keys(Keys.RETURN)

I am new to selenium so excuse any redundancy and thank you in advance.

Upvotes: 0

Views: 725

Answers (1)

frianH
frianH

Reputation: 7563

That's a dynamic id, try with another way, like this:

tools = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@data-qtip='Tools']")))
tools.click()

text = "Search";
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='" +text +"']")))
element.click()

Upvotes: 1

Related Questions