I am coding a bot for tinder with selenium but its not working. Here is my code.
fb_button = self.driver.find_element_by_xpath('//*[#id="content"]/div/div[1]/div/div/main/div/div[2]/div[2]/div/div/span/div[2]/button')
fb_button.click()
This code is for clicking the facebook login button. However when i run the code, it dont work healthy.I am getting an error like this.
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate e
lement: {"method":"xpath","selector":"//*[#id="content"]/div/div[1]/div/div/main/div/div[2]/div
[2]/div/div/span/div[2]/button"}
However when i try to create an object and try to call this function, it returns something.
HTML of the button:
<button type="button" class="button Lts($ls-s) Z(0) CenterAlign Mx(a) Cur(p) Tt(u) Bdrs(100px) Px(24px) Px(20px)--s Py(0) Mih(42px)--s Mih(50px)--ml button--outline Bdw(2px) Bds(s) Trsdu($fast) Bdc(#fff) C(#fff) Bdc(#fff):h C(#fff):h Bdc(#fff):f C(#fff):f Bdc(#fff):a C(#fff):a Fw($semibold) focus-button-style W(100%) Fz(4vw)--ms" draggable="false" aria-label="Facebook ile oturum aç"><span class="Pos(r) Z(1) D(ib)">Facebook ile oturum aç</span></button>
you can wait until the element can clickable and present in HTML dom.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
fb_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "your_xpath")))
fb_button.click()
you can check the wait conditions here!
You can click on the element using xpath:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Facebook ile oturum aç']"))).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
Related
button = self.driver.find_element_by_xpath(
"/html/body/div[4]/div[2]/div/div[1]/main/div/div[2]/div/div/div/form/div[3]/button")
button.click()
when I try to run my code I get the exception: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[4]/div[2]/div/div[1]/main/div/div[2]/div/div/div/form/div[3]/button"}
(Session info: chrome=91.0.4472.164
meanwhile its the xpath of the button which is found https://www.zalando.fr/login
and this is the element
Se connecter
i've tried finding it by class name and everything it just doesn't work
Your locator is wrong.
Also use explicit wait.
Try this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[data-testid='login_button']"))).click()
you can try this where need to handle the cookie window which is getting displayed after the page is getting loaded and used explicitWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
driver.get("https://www.zalando.fr/login")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='OK']"))).click()
#here I'm passing the user name
driver.find_element_by_xpath("//*[#id='login.email']").send_keys("email")
#here passing password
driver.find_element_by_xpath("//*[#id='login.password']").send_keys("password")
#clicking on the `Se connecter` button
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#data-testid='login_button']"))).click()
The xpath ended up being wrong. I copied it from chrometools and tried later with brave and I got the correct xpath.
I have a program which uses the python selenium webdriver and I get the following runtime error:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#id='id_login']"}
(Session info: chrome=83.0.4103.1
HTML
<input type="text" name="login" placeholder="Type your username" required="" id="id_login" xpath="1">16)
code:
from selenium import webdriver
#from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path="c:\\Chrome1\chromedriver.exe")
driver.get("https://www.jstor.org")
print(driver.title)
driver.find_element_by_xpath("//a[#class='inline-block plm']").click()
driver.find_element_by_xpath("//input[#id='id_login']").send_keys('xxxxxx#gmail.com')
This error means that selenium could not localize the element because it was not on the site or it did not load. I suggest you using the WebdriverWait() function. It will wait X seconds until the element is clickable. If it will still not be, it will throw an error.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(self.webdriver, 60).until(EC.element_to_be_clickable((By.XPATH, '//a[#class='inline-block plm']')))
More effective if you direct directly to this url:
https://www.jstor.org/action/showLogin?redirectUri=/
And use selenium wait to solve your issue.
driver.get('https://www.jstor.org/action/showLogin?redirectUri=/')
user_name = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='id_login']")))
user_name.send_keys('xxxxxx#gmail.com')
Although your xpath will work, using an id looks better .element_to_be_clickable((By.ID, "id_login"))
You need following import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
To send a character sequence to the Username field you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.jstor.org/")
ebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.inline-block.plm"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#id_login"))).send_keys("SunilTirupathi#stackoverflow.com")
Using XPATH:
driver.get("https://www.jstor.org/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='inline-block plm']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='id_login']"))).send_keys("SunilTirupathi#stackoverflow.com")
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
Browser Snapshot:
References
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
XPath doesn't work on below path code.
HTML
<span class="voter_details">
<h4 class="v_msg" id="v_msg" style="font-size: 14px;margin-bottom:25px;">Hi, you are here XXXfor in a while, kindly enter your name!</h4>
<input value="" placeholder="Enter your Name" type="text" id="v" name="v" class="form-control name_box" maxlength="20">
</span>
Error
selenium.common.exceptions.NoSuchElementException: Message: no such element:
Unable to locate element: {"method":"xpath","selector":"//*[#id='v']"}
Code
driver.find_element(driver.find_element(By.XPATH, "//*[#id='v']")).send_keys('random.choice(list)')
Use following xpath to access the input element.
driver.find_element_by_xpath("//input[#placeholder='Enter your Name'][#id='v']").send_keys('Ashwani')
OR You can use WebdriverWait to wait for the element to be clickable and send then enter the value.
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[#placeholder='Enter your Name'][#id='v']"))).send_keys('Ashwani')
To execute that you need to have following imports.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
To send a character sequence you you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control.name_box#v[placeholder='Enter your Name']"))).send_keys(random.choice(list))
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='form-control name_box' and #id='v'][#placeholder='Enter your Name']"))).send_keys(random.choice(list))
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
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="uname"]"}
I got same error while I was working with Selenium.
The problem is slow internet/big website. Try adding explicit or implicit delay or you may import time module and add time.sleep before clicking/sending keys
Traceback (most recent call last):File
"C:\Users\PSWN672P\AppData\Local\Programs\Python\Python37\Python
programs\SNOW1.py", line 17, in
EC.element_to_be_clickable((By.CSS_SELECTOR,'ul[id*="collapseId"]>li:nth-child(5)>ul[id*="collapseId"]>li>div>a>div>div'))
File "C:\Users\PSWN672P\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\support\wait.py",
line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Trying to access the element with text as My Groups Work and run a script to automatically click that element and navigate to next page:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
import time
browser=webdriver.Ie()
browser.get('http://example.com')
try:
window_before=browser.window_handles[0]
element=WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'ul[id*="collapseId"]>li:nth-child(5)>ul[id*="collapseId"]>li>div>a>div>div')))
element.click()
time.sleep(15)
window_after=browser.window_handles[1]
browser.switch_to_window(window_after)
finally:
browser.quit()
<a class="sn-widget-list-item sn-widget-list-item_hidden-action module-node" id="2ccb50dfc61122820032728dcea648fe" href="task_list.do?sysparm_userpref_module=2ccb50dfc61122820032728dcea648fe&sysparm_query=assignment_group=javascript:getMyGroups()^active=true^assigned_to=^sys_class_name!=cert_follow_on_task^sys_class_name!=sc_req_item^sys_class_name!=sc_request^EQ&sysparm_clear_stack=true" target="gsft_main"><div class="sn-widget-list-content" data-id="2ccb50dfc61122820032728dcea648fe">
<div class="sn-widget-list-content" data-id="2ccb50dfc61122820032728dcea648fe">
<div class="sn-widget-list-title">My Groups Work</div>
</div>
</a>
why not just locate element by class name?
element=WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CLASS_NAME,'sn-widget-list-title')))
CSS Selector throws timeout exception as the Locator Strategy which you have adapted doesn't identifies the desired element uniquely. Perhaps TimeoutException is the outcome of failed ExpectedConditions.
However, to click() on the element with text as "My Groups Work" you can use the following solution:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.sn-widget-list-item.sn-widget-list-item_hidden-action.module-node[href^='task_list.do?sysparm_userpref_module=']>div.sn-widget-list-content[data-id]>div.sn-widget-list-title")))
Alternative
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='sn-widget-list-item sn-widget-list-item_hidden-action module-node' and starts-with(#href,'task_list.do?sysparm_userpref_module=')]/div[#class='sn-widget-list-content' and (#data-id)]/div[#class='sn-widget-list-title' and contains(., 'My Groups Work')]")))
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
I have a problem, I happen to have a button with these labels, I'm trying to click through Selenium but I can not find a way to click it. I tried to give it taking as reference the XPath, link text, and CSS selector but I do not achieve my goal. This is the code for the button:
<a class="btn btn-flat pull-right" data-action="export_report"> <i class = "icon-export"> </ i> Export </a>
East of the XPath:
// * [# id = "reports"] / div [1] / div [2] / a
this selector:
#reports> div.span12> div.headline-action-block.pull-right> a
This is the button and my code in Python :(
Button:
My code:
I face this error:
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 955, in find_element
'value': value})['value']
You can try this:
openrate = driver.find_element_by_css_selector("a.btn")
openrate.click()
Assuming this is the only button, or the first button on the page. Otherwise ("a.btn.btn-flat.pull-right")
As per the HTML you have shared it seems you have invoked click() in the previous step so in this step to click() on the button with text as Export you have to induce WebDriverwait for the element to be clickable and you can use either of the following Locator Strategies :
PARTIAL_LINK_TEXT :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Export"))).click()
CSS_SELECTOR :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-flat.pull-right[data-action='export_report']>i.icon-export"))).click()
XPATH :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn-flat pull-right' and #data-action='export_report']/i[#class='icon-export']"))).click()