How to locate the span element with Xpath Selenium and Python - python-3.x

My HTML format is below:
***button class="btn authorize unlocked"
span>Authenticate</span
button***
So when I use "browser.find_element_by_xpath('//span[#class = "btn authorize unlocked"]')"
to locate this button, it can not find that.
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[#class = "btn authorize unlocked"]"}
So what should I change?

To locate the element you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element(By.CSS_SELECTOR, "button.btn.authorize.unlocked > span")
Using xpath:
element = driver.find_element(By.XPATH, "//button[#class='btn authorize unlocked']/span[text()='Authenticate']")
Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn.authorize.unlocked > span")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[#class='btn authorize unlocked']/span[text()='Authenticate']")))
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

Need CSS pattern for the selenium in python to locate the element

This is the HTML syntax but I want to locate the element using value "ADD QUERY" using find_element_by_css_selector.
Image of HTML:
To locate the element you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = driver.find_element(By.CSS_SELECTOR, "div.global-button-primary.telemetry-button")
Using XPATH:
element = driver.find_element(By.XPATH, "//div[contains(#class, 'global-button-primary') and contains(#class, 'telemetry-button')][contains(., 'ADD QUERY')]")
Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.global-button-primary.telemetry-button")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(#class, 'global-button-primary') and contains(#class, 'telemetry-button')][contains(., 'ADD QUERY')]")))
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

Press button using selenium on yahoo finance doesn't work

I am trying to get the top stocks for the day so I go to https://finance.yahoo.com/gainers but I the want to edit the filters by pressing Edit.
driver = webdriver.Chrome()
driver.get("https://finance.yahoo.com/gainers")
element = driver.find_element_by_class_name("Bgc($linkColor).Bgc($linkActiveColor):h.C(white).Fw(500).Px(20px).Py(9px).Bdrs(3px).Bd(0).Fz(s).D(ib).Whs(nw).Miw(110px)")
element.click()
This doesn't work. How can I fix it?
To click on the element Edit you can use either of the following Locator Strategies:
Using xpath:
driver.get("https://finance.yahoo.com/gainers")
driver.find_element_by_xpath("//span[text()='Edit']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH:
driver.get("https://finance.yahoo.com/gainers")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Edit']"))).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
The java code below seems to work.
WebDriver driver = new ChromeDriver();
driver.get("https://finance.yahoo.com/gainers");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement editButton = wait
.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[data-reactid=\"23\"]")));
editButton.click();
Cleaner locator
WebElement editButton = wait
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button/span[contains(text(),'Edit')]")));

Cannot find the css selector, method or element error using Selenium and Python

<div class="dijitReset dijitInline dijitMenuItemLabel cpNavLeftLink dijitMenuItemSelected dijitMenuItem" data-dojo-attach-point="focusNode" role="menuitem" tabindex="-1" data-dojo-attach-event="onmouseenter:_onHover,onmouseleave:_onUnhover,ondijitclick:_onClick" aria-labelledby="dijit_PopupMenuBarItem_2_text" id="dijit_PopupMenuBarItem_2" aria-disabled="false" widgetid="dijit_PopupMenuBarItem_2" aria-haspopup="true" style="">*
<span data-dojo-attach-point="containerNode" id="dijit_PopupMenuBarItem_2_text">Carload Tools</span>
</div>
I am trying to access the following element using selenium but for some reason it saying that they are not able to recognize this element please help. My code is working fine for other elements but for this one its not able to find the element.
To identify the element you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element_by_css_selector("div#dijit_PopupMenuBarItem_2[widgetid='dijit_PopupMenuBarItem_2']>span#dijit_PopupMenuBarItem_2_text[data-dojo-attach-point='containerNode']")
Using xpath:
element = driver.find_element_by_xpath("//div[#id='dijit_PopupMenuBarItem_2' and #widgetid='dijit_PopupMenuBarItem_2']/span[#id='dijit_PopupMenuBarItem_2_text' and #data-dojo-attach-point='containerNode']")
Ideally, to locate any element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div#dijit_PopupMenuBarItem_2[widgetid='dijit_PopupMenuBarItem_2']>span#dijit_PopupMenuBarItem_2_text[data-dojo-attach-point='containerNode']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#id='dijit_PopupMenuBarItem_2' and #widgetid='dijit_PopupMenuBarItem_2']/span[#id='dijit_PopupMenuBarItem_2_text' and #data-dojo-attach-point='containerNode']")))
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
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

selenium.common.exceptions.NoSuchElementException error trying to identify element to click using Selenium and Python

despite numerous tries I'm unable to perform the following action(s).
I need to land on a page that contains one or more table rows/columns. For each (sequentially) I need to click on an arrow that opens a pop-up, close the window, then rinse and repeat.
Problem: I am unable to click on the item
Error:
class 'selenium.common.exceptions.NoSuchElementException'
Snippet of code that prompts the error:
[...]
driver = webdriver.Chrome('chromedriver.exe')
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
# MODIFY URL HERE
driver.get(url)
[..]
try:
# arf = driver.find_element_by_name("ctl00_ContentPlaceHolder1_d_dettaglio_ctl02_button1").click()
arf = driver.find_element_by_xpath('//input[#id="ctl00_ContentPlaceHolder1_d_dettaglio_ctl02_button1"]').click()
pprint.pprint(arf)
except NoSuchElementException:
print ("error!", NoSuchElementException)
driver.close()
exit()
HTML element I need to interact with:
<td align="center">
<input type="image" name="ctl00$ContentPlaceHolder1$d_dettaglio$ctl02$button1" id="ctl00_ContentPlaceHolder1_d_dettaglio_ctl02_button1" src="../images/spunta_maggiore.gif" style="height:22px;width:22px;border-width:0px;">
</td>
Things I've tried:
driver.find_element_by_xpath (//input[#id etc...]) or driver.find_element_by_xpath('//input[#name])
driver.find_element_by_name("ctl00_ContentPlaceHolder1_d_dettaglio_ctl02_button1").click()
driver.find_element_by_id("ctl00_ContentPlaceHolder1_d_dettaglio_ctl02_button1").click()
In both cases, a no element found exception is raised.
What am I doing wrong ?
The desired element is a dynamic element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='image'][name*='ContentPlaceHolder'][id*='d_dettaglio'][src*='images/spunta_maggiore']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(#name, 'ContentPlaceHolder') and contains(#id, 'd_dettaglio')][#type='image' and contains(#src, 'images/spunta_maggiore.gif')]"))).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
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

ElementNotInteractableException: Message: Element could not be scrolled into view using GeckoDriver Firefox with Selenium WebDriver

How can I fix the error:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <> could not be scrolled into view
error when working with Firefox via Selenium?
None of the tips from the site did not help me. I tried all the solutions I could find, including through WebDriverWait and JS. One of the solutions gave:
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (568, 1215) is out of bounds of viewport width (1283) and height (699)
I tried resizing the browser window, which also didn't work.
My code:
webdriverDir = "/Users/Admin/Desktop/MyVersion/geckodriver.exe"
home_url = 'https://appleid.apple.com/account/'
browser = webdriver.Firefox(executable_path=webdriverDir)
browser.get(home_url)
browser.find_element_by_css_selector("captcha-input").click()
A solution that throws a window size error:
actions = ActionChains(browser)
wait = WebDriverWait(browser, 10)
element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "captcha-input")))
actions.move_to_element(element).perform()
element.click()
By the way, this same code works perfectly in Chrome. But it's obvious enough.
To send a character sequence to the <captcha-input> 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://appleid.apple.com/account#!&page=create')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.captcha-input input.generic-input-field"))).send_keys("JohnTit")
Using XPATH:
driver.get('https://appleid.apple.com/account#!&page=create')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//captcha-input//input[#class='generic-input-field form-textbox form-textbox-text ']"))).send_keys("JohnTit")
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:

Resources