I am trying to download protein sequences from Uniprot with the following code.
driver = webdriver.Chrome(driver_location)
#get website
driver.get('https://www.uniprot.org/uniprotkb/P19515/entry#sequences')
#stall to load webpage
time.sleep(5)
#scroll webpage
#driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
#create instance of button and click
button = driver.find_element_by_link_text("Copy sequence")
button.click()
Running the previous block of code returns the following error
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Copy sequence"}
Additionally, here is the css layout enter image description here
I assume the problem is the button is either dynamic or hidden in some way that the webdriver cannot locate the button. I know there is a Uniport API and probably other more efficient ways to download protein sequences but for the sake of learning how can I modify my code and why isn't the button clickable?
CodePudding user response:
Link text only works if the locator has a hyperlink, so in this case it wont work since its a button which copies the the text into the clipboard and doesn't a href
tag.
however you can modify your code to
button = driver.find_element(By.CSS_SELECTOR, "button.button.primary.tertiary")
and it will perform the click operation.
CodePudding user response:
link_text
only works for a
tags, not button
tags. But if you use the SeleniumBase framework on GitHub, there's a special TAG:contains("TEXT")
selector that you can use to click the button.
Here's a working script: (After installing seleniumbase
using pip install seleniumbase
, run the script with python
or pytest
)
from seleniumbase import BaseCase
class RecorderTest(BaseCase):
def test_recording(self):
self.open("https://www.uniprot.org/uniprotkb/P19515/entry#sequences")
self.click('button:contains("Copy sequence")')
self.sleep(3)
if __name__ == "__main__":
from pytest import main
main([__file__])
You can use the SeleniumBase Recorder to generate a script like that from manual browser actions. The last part with if __name__ == "__main__":
lets you run the script with python
instead of just pytest
.