Reputation: 19
I am a newbie in selenium, and I got stock when I want to click the button with the next html code :
<div class="mb-8">
<div class="wrapper">
<button class="w-full h-14 pt-2 pb-1 px-3 bg-accent text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small ">
<div class="font-medium">
<div class="text-17 md:text-18 md:font-bold leading-18">Activate</div>
<div class="text-13 md:text-12 font-normal md:font-medium leading-normal">Mining 4 hours</div>
</div>
</button>
</div>
</div>
This is what I tryed :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import time
driver = webdriver.Chrome("chromedriver.exe")
driver.get("https://link")
search = driver.find_element_by_id("email")
search.clear()
search.send_keys("username")
search = driver.find_element_by_id("password")
search.clear()
search.send_keys("password")
search.send_keys(Keys.RETURN)
time.sleep(3)
link= driver.find_element_by_link_text("Miner")
link.click()
time.sleep(3)
link= driver.find_element_by_class_name("w-full h-14 pt-2 pb-1 px-3 bg-accent text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small ")
Got this:
InvalidSelectorException: invalid selector: An invalid or illegal selector was specified
(Session info: chrome=88.0.4324.182)
link=driver.find_element_by_css_selector("w-full h-14 pt-2 pb-1 px-3 bg-accent text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small ")
Same error. Tryed also this (because of the info in the image)
link=driver.find_element_by_css_selector(".w-full.h-14.pt-2.pb-1.px-3.bg-accent.text-dark-1.rounded-full.md\:rounded.select-none.cursor-pointer.md\:hover\:shadow-big.focus\:outline-none.md\:focus\:bg-accent-2.md\:focus\:shadow-small.")
Same error. Tryed this also:
link= driver.find_element_by_class_name("Activate")
Got this :
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".Activate"}
(Session info: chrome=88.0.4324.182)
Thank you for your help !!!
Upvotes: 1
Views: 456
Reputation: 45
If you're still struggling with this task I have posted the answer on a different post Find the button element for selenium Automation in python be sure to check it out.
Upvotes: 1
Reputation: 11
If you want to find an element by class then just use one class instead of all classes.
Here in your case 'Activate' is not a class. You can try:
link= driver.find_element_by_class_name("w-full");
Also, you can find this button using XPath.
Upvotes: 1
Reputation: 9969
If none of these work your element is in an iframe. driver.switch_to.frame() would solve it.
Xpaths:
//div[@class='wrapper']/button
//div[.='Activate']/parent::button[1]
Css selectors:
button.w-full.h-14.pt-2.pb-1.px-3.bg-accent.text-dark-1.rounded-full.md:rounded.select-none cursor-pointer.md:hover:shadow-big.focus:outline-none.md:focus:bg-accent-2 .md:focus:shadow-small
Upvotes: 2