I'm trying to use selenium to click on a checkbox, as is depicted in the HTML of the picture. The think is, the checkbox input tag is the same for all four options, and they are distinguished only through the id="country_england" ... . I'm not sure how I can write code to select the england box and click it. Any help appreciated.
HTML Code:
use xpath:
//b[#id="country_england"]/../input
This finds the p tag that has that ID and then goes to the parent element and navigates back to input element
you can also use:
//b[#id="country_england"]/preceding-sibling::input
This finds the preceding element of p tag that has tag input
Code would be :
webD.get("https://icem.data-archive.ac.uk/#step1")
WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
(By.XPATH, '//*[#value="1861"]'))).click()
WebDriverWait(webD, 20).until(EC.element_to_be_clickable(
(By.XPATH, '//b[#id = "country_england"]/preceding-sibling::input'))).click()
The desired element is a Angular element. So 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 and preceding:
driver.get("https://icem.data-archive.ac.uk/#step1")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#value='1851']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//b[#class='ng-binding' and #id='country_england']//preceding::input[1]"))).click()
Using XPATH and preceding-sibling:
driver.get("https://icem.data-archive.ac.uk/#step1")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#value='1851']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//b[#class='ng-binding' and #id='country_england']//preceding-sibling::input[1]"))).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
Browser Snapshot:
Update
Related
I am now convinced that there is no solution to this. But in case there is someone out there who can help: Whole Element that I want to click on genesys cloud is (Its a part of a table):
<a target="_blank" data-bind="
attr: {
href: lastReportRun().reportUrl
},
lAttr: {
title: 'reports.list.grid.downloadColumn.fileTypes.' + lastReportFileType()
},
lString: 'reports.list.grid.downloadColumn.fileTypes.' + lastReportFileType()
" href="https://apps.usw2.pure.cloud/platform/api/v2/downloads/9161911a0307202a" title="XLSX">XLSX</a>
Element snapshot:
I am trying to locate this element to click on it and start downloading the .xlsx file. These are the things I have tried so far, but no luck. Can someone please help me understand where I can correct it.
Code trials:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "XLSX"))).click()
driver.find_element_by_link_text("XLSX").click()
driver.find_element_by_xpath(u'//a[text()="XLSX"]').click()
driver.find_element_by_xpath('//a[normalize-space(text())="XLSX').click()
driver.find_element(By.XPATH, "//input[#name='XLSX' and #value='XLSX']").click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH, "//*input[#name='XLSX' and #value='XLSX']"))).click()
driver.find_element_by_xpath('/html/body/div[1]/div/div/div/div/div/div/div/div/div[2]/div[2]/div/div[5]/a').click()
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//*span[contains(., 'XLSX')]"))).click()
l=driver.find_element_by_xpath("//*a[#title='XLSX']")
l.click()
The desired element is a dynamic <a> element.
So 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 LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "XLSX"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href^='https://apps.usw2'][title='XLSX'][data-bind]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(#href, 'https://apps.usw2') and text()='XLSX'][#title='XLSX' and #data-bind]"))).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 tried to click link from selenium.webdriver but I got it nothing. Can please help with this issue
Page contains the following elements:
Details
My target to click:
"m_m_cBody_bdy_uc_tbl$Edit","13911"
Please note that I have many text links with
Details
is not a unique element on my page
<a href="javascript:__doPostBack("m_m_cBody_bdy_uc_tbl$Edit","41946")">
Details </a>
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 and normalize-space():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[normalize-space()='Details' and contains(#href, '13911')]"))).click()
Using XPATH and contains():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'Details') and contains(#href, '13911')]"))).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 was making a webscraper to get gpu stocks from https://www.nvidia.com/en-us/shop/geforce/?page=1&limit=9&locale=en-us to get my hands on a 30 series card, I am using python with bs4 and selenium for this.
I want to load more shopping items, on the website it has this load more button. So I grabbed its class and made it so selenium clicks it:
driver.find_element_by_class_name("buy-link").click()
but it says that the element in non interactiable, HTML for the button
The exact error it gives me is:
Message: element click intercepted: Element <span class="extra_style buy-link" data-color="#76b900" data-secondary-color="#fff" style="visibility: visible; cursor: pointer;" data-mpn-code="NVGFT070">...</span> is not clickable at point (657, 594). Other element would receive the click: <div class="popBg" id="nv-buy-now-model" style="display: block;">...</div>
I don't know much HTML, how can I achieve clicking this button
The LOAD MORE element is a Angular element so to click on it you need 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.nvidia.com/en-us/shop/geforce/?page=1&limit=9&locale=en-us")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#cookiePolicy-btn-close>span"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.buy-link.load-more-btn[value='LOAD MORE']"))).click()
Using XPATH:
driver.get("https://www.nvidia.com/en-us/shop/geforce/?page=1&limit=9&locale=en-us")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='cookiePolicy-btn-close']/span"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='buy-link load-more-btn' and #value='LOAD MORE']"))).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
Browser Snapshot:
The problem is that there are some 20 elements on that page that match your first locator, .find_element_by_class_name("buy-link"). In this case, if you make your locator more specific, you can isolate it down to just the "LOAD MORE" INPUT button.
Try this instead
driver.find_element_by_css_selector("input.buy-link").click()
It's generally a good practice to add a wait unless you are sure you won't ever need it.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='CLOSE']")).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.buy-link")).click()
See the docs for more info and details.
I want to click on "create post" button of this element:
<div class="l61y9joe j8otv06s a1itoznt qwtvmjv2 kiex77na lgsfgr3h mcogi7i5 ih1xi9zn ippphs35 a53abz89" data-hover="tooltip" data-tooltip-display="overflow" id="js_2f">Create Post</div>
But this id (id="js_2f") has a random value:
I did it this way but didn't work !
wait = WebDriverWait(self.browser, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//*[text()='Create Post' and contains(#id, 'js_')]"))).click()
How can i click on it Using Xpath method ?
Following should help:
driver.find_element_by_xpath('//div[contains(text(),"Create Post")]').click()
You can try below xpath with contains
//div[contains(text(),'Create Post')]
You can try to perform a click on this element using link text
driver.find_element_by_link_text("Create Post")
or
driver.find_element_by_xpath("//div[text() = 'Create Post']")
The Create Post button 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 Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[data-hover='tooltip'][data-tooltip-display]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#data-hover='tooltip' and text()='Create Post'][#data-tooltip-display]"))).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'm trying to click a link in a dropdown menu in Selenium.
I'm accessing the element like so:
link = menu.find_element_by_xpath('//*[contains(text(), "Mark as shipped")]')
The link's href is javascript.void(0), and contains an onclick attribute which contains:
'com.ebay.app.myebay2.lineaction.service.LineActionAjax.processTransRequest("http://payments.ebay.com/ws/eBayISAPI.dll?OrderAction&transId=#TID#&action=4&pagetype=1883&ssPageName=STRK:MESO:SHP&itemid=_Item_Id", "_Item_Id", "987349587", "MarkShipped", "98739873", "_Item_Id_9874987_ss", 24")'
I've tried triggering this with:
click()
and
driver.execute_script(link.get_attribute('onclick'))
Also an ActionChain mousing over the link and clicking it.
But none seem to work. How do I trigger this?
The element is a AJAX element, so 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 PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Mark as shipped"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick*='MarkShipped']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#onclick, 'MarkShipped') and contains(., 'Mark as shipped')]"))).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