I can't locate element on website and insert numerical value inside using Selenium on Firefox with Python.
I am using firefox add-in to get the xPath for the input field, I receive:
/html/body/div1/section/div/div/div/div/form/div1/div/div[4]/div2/div/div/div/div1/div1/table/tbody/tr1/td3/div/input
however with Python code objDriver.find_element_by_xpath("...") - I receive "Unable to locate element:" -> like xpath is wrong
If I use it on the let's say border of the input field, I get following xPath:
/html/body/div1/section/div/div/div/div/form/div1/div/div[4]/div2/div/div/div/div1/div1/table/tbody/tr1/td3
This element I can allocate using objDriver.find_element_by_xpath("...") but it is not input field, so I can't insert value
If I click "Inspect" in Firefox, I get:
CSS Path:
html body div.main-content section.section div.block-one_col_1 div.container-fluid.wrapper div.row div.col-12 form#id_form-process.form-horizontal.formfactory div.row div.col-md div#fieldgroup_variants.fieldset div#fieldgroup_variants__fields.fieldset__content div.form-group.row.formfield_variantsCalculator.fieldpk_2533.mb-0.is-valid div.col-md-6.col-12 div.d-flex.flex-row div.w-100 div.table-scroll table.table.table--step tbody#tableContent.table tr.table-row.table-row--count td div.form-group.group1 input.form-control.variant-value.variant1.touched
xPath:
/html/body/div/section/div/div/div/div/form/div[1]/div/div[4]/div[2]/div/div/div/div[1]/div[1]/table/tbody/tr[1]/td[3]/div/input
HTML part I am interested in:
<td data-risk="6.1"><div class="form-group group1" data-excluded="false" data-risk="6.1"><input name="1" class="form-control variant-value variant1 touched" data-editable="true" data-risk="6.1" data-visible-sum="true" data-dynamic="false"></div></td>
Do you have any idea how can I locate the field I need?
General view of Input field:
When getting xPath of the border:
When getting xPath of the Input field:
Can you try with the below xpath with Explicit waits :
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#name= '1' and contains(#class, 'variant-value')]"))).send_keys('10000')
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try with xPath = //input[#class="form-control variant-value variant1 touched"][#id="1"]
Related
I am new to Webelement and selenium, can anyone help me on how to locate element below , using text "Hotel Wahington":
<a class="hotel_name_link url" href="/hotel/nl/washington.html?label=gen173nr-1FCAEoggI46AdIM1gEaFCIAQGYATG4ARjIAQzYAQHoAQH4AQKIAgGoAgS4Apyz__EFwAIB&sid=1b691d9ad57ac7ee7c3d40dac2f7f488&dest_id=-2140479&dest_type=city&group_adults=2&group_children=0&hapos=1&hpos=1&no_rooms=1&sr_order=popularity&srepoch=1581242789&srpvid=87de4712b9a90095&ucfs=1&from=searchresults;highlight_room=#hotelTmpl" target="_blank" rel="noopener">
<span class="sr-hotel__name" data-et-click=" ">Hotel Washington</span>
<span class="invisible_spoken">Opens in new window</span>
</a>
Adding the original link below :
https://www.booking.com/searchresults.html?label=gen173nr-1FCAEoggI46AdIM1gEaFCIAQGYATG4ARjIAQzYAQHoAQH4AQKIAgGoAgS4ApSSgPIFwAIB&sid=dcd3ff4c30abfd0bc264d5ae4a4c1d7a&sb=1&sb_lp=1&src=index&src_elem=sb&error_url=https%3A%2F%2Fwww.booking.com%2Findex.html%3Flabel%3Dgen173nr-1FCAEoggI46AdIM1gEaFCIAQGYATG4ARjIAQzYAQHoAQH4AQKIAgGoAgS4ApSSgPIFwAIB%3Bsid%3Ddcd3ff4c30abfd0bc264d5ae4a4c1d7a%3Bsb_price_type%3Dtotal%26%3B&sr_autoscroll=1&ss=hotel+washington&is_ski_area=0&checkin_year=&checkin_month=&checkout_year=&checkout_month=&group_adults=2&group_children=0&no_rooms=1&b_h4u_keep_filters=&from_sf=1
To select anchor tag based on text "Hotel Wahington" use the following xpath.
Use following Xpath
driver.find_element_by_xpath("//a[#class='hotel_name_link url' and contains(.,'Hotel Washington')]").click()
Or following css selector.
driver.find_element_by_css_selector("a.hotel_name_link.url[href*='/hotel/nl/washington']").click()
Or partial link text.
driver.find_element_by_partial_link_text("Hotel Washington").click()
Tryout this xpath:
//span[text()='Hotel Washington']/parent::a
Also note that the method "find_element_by_xpath()" in the answers is from version 4 of Selenium which is still in Alpha testing phase. You will be most likely using Selenium version 3. The valid code for version 3 would be:
driver.findElement(By.xpath("//span[text()='Hotel Washington']/parent::a")),click();
Using xpath you can go to parent element with ..
driver.find_element_by_xpath('//span[.="Hotel Washington"]/..')
Or locate an element which has specific text
driver.find_element_by_xpath('//a[span[.="Hotel Washington"]]')
Use the
xPath = //*[contains(#text(),'Hotel Wahington')]
OR
//a[#class='hotel_name_link url']//*[contains(#text(),'Hotel Wahington')]
You can locate an element using text only. You can try below solution:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 50).until(
EC.presence_of_element_located((By.XPATH, "//span[contains(., 'Hotel Washington')]")))
element.click()
I am trying to enter text into a text box on a web page but I am unable to locate it. I've tried finding it by xpath using the placeholder but may have had the wrong syntax as it was throwing an error.
The error I get with the below python is element not interactable - but it's a text field? surely it's intractable?
HTML:
<div class="name-filter-input"> == $0
<input placeholder="Search" ng-model="filter"
class="ng-pristine ng-valid">
</div>
My Python:
browser.find_element_by_class_name('name-filter-input')
campaign_text_field = browser.find_element_by_class_name('name-filter-input')
campaign_text_field.send_keys(x)
It seems you are trying to interact with the division instead of the input field.
Could you try changing the class name as so:
campaign_text_field = browser.find_element_by_class_name('ng-pristine ng-valid')
Use CSS Selector for the input element.
campaign_text_field = browser.find_element_by_css_selector('input.ng-pristine.ng-valid')
campaign_text_field.send_keys("XXX")
For the best practice induce WebDriverWait() and wait for element_to_be_clickable()
WebDriverWait(browser,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'input.ng-pristine.ng-valid'))).send_keys("XXX")
Add following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
I try to click on html a tag with selenium but I get an error.
How can I get selenium to click on such tag?
I've tried this code but I get an error:
driver.find_element_by_id("btnCreateJE").click()
HTML code:
<a id="btnCreateJE" data-permission="true" onclick="NewManualJE()" href="#" class="btn waves-effect waves-light"> New </a>
Python selenium code:
driver.find_element_by_id("btnCreateJE").click()
I get that error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="btnCreateJE"]"}
(Session info: chrome=75.0.3770.142)
Make sure that the element doesn't belong to the iframe, if this is the case - you will need to switch_to_frame first
Make sure that the element doesn't belong to the Shadow DOM, if this is the case - you will have to find the relevant ShadowRoot and locate your element relatively
It might be the case the link is not immediately present in DOM, i.e. it's being added later as a result of an AJAX call. If this is the case - consider adding Explicit Wait like:
WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.ID, "btnCreateJE"))).click()
The desired element is a JavaScript enabled 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 solutions:
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "New"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.waves-effect.waves-light#btnCreateJE[onclick^='NewManualJE']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn waves-effect waves-light' and #id='btnCreateJE'][starts-with(#onclick, 'NewManualJE')]"))).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
I want to locate (and click) the "Reoni" element, but I do not know what function to use it for
I tried with
driver.find_element_by_class_name("oe_menu_leaf")
and
driver.find_element_by_class_name("oe_menu_text")
but then selenium raise an error element cant be located,
and I tried
driver.find_element_by_link_text("Reoni")
This is the element I want to locate:
<a href="/web#menu_id=86&action=99" class="oe_menu_leaf" data-menu="86" data-action-model="ir.actions.act_window" data-action-id="99">
<span class="oe_menu_text">
Reoni
</span>
</a>
and full html:
If I was not clear enough or if you needed my code, please let me know.
As the desired element is a dynamic element you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.oe_menu_leaf[href*='/web#menu_id=']>span.oe_menu_text"))).click()
Using XPATH and text():
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='oe_menu_leaf' and starts-with(#href,'/web#menu_id=')]/span[#class='oe_menu_text' and text()='Reoni']"))).click()
Using XPATH and normalize-space():
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='oe_menu_leaf' and contains(#href,'/web#menu_id=')]/span[#class='oe_menu_text' and normalize-space()='Reoni']"))).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
Reference
You can find a relevant detailed discussion in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
Try something like this:
Clicking the button
From Chrome :
Right click "inspect" on the item you are trying to find the xpath.
Right click on the highlighted area on the console.
Go to Copy xpath
selectElem=browser.find_element_by_xpath('x-path-here').click()
Reading Values Only
from bs4 import BeautifulSoup
innerHTML = browser.execute_script("return document.body.innerHTML")
soup = BeautifulSoup(str(innerHTML.encode('utf-8').strip()), 'lxml')
value = soup.find('span', attrs={'class':'fxst-calendarpro fxst-table-s1'}).text
Here is the element:
<label title="Place Order" style="cursor: pointer" onclick="AjaxPageLoad(fno, 'FNOOrder');">Place Order<div></div></label>
I am trying to click on this but need to use the #onclick as the main differentiator. I read the other similar questions but can't seem to get this to work:
driver.find_element_by_xpath("//a[contains(#onclick,'fno')]").click()
driver.find_element_by_xpath("//a[contains(#onclick,'FNOOrder')]").click()
Error I get:
ElementNotInteractableException: Message: Element <a id="lnkFNO" class="active" href="javascript:void(0)"> could not be scrolled into view
But the element is visible so I assume that the xpath must be wrong.
As per the HTML you have shared to click on the desired element using the onclick event you have to induce WebDriverWait and you can use the follwing solution:
xpath using onclick:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[#onclick=\"AjaxPageLoad(fno, 'FNOOrder');\"]"))).click()
xpath using onclick and title:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[#title='Place Order'][#onclick=\"AjaxPageLoad(fno, 'FNOOrder');\"]"))).click()
xpath using onclick, title and innerHTML:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[#title='Place Order'][#onclick=\"AjaxPageLoad(fno, 'FNOOrder');\"][contains(.,'Place Order')]"))).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
Try to use css selector, if you are familiar with web development, it should be easier.
driver.find_element_by_css_selector("label[onclick*=fno]").click()
Above code will select label with onclick argument contains 'fno' string. Here you can find all selectors: https://www.w3schools.com/cssref/css_selectors.asp