document.querySelector( ).click(); not working selenium $0.click(); undefined - python

I am trying to click a button with selenium. I have tried Chrome and Firefox console with $0.click(); and works but when testing with the selector it does not work, "undefined", document.querySelector("#td").click();.
I trying with selenium does not work.
HTML:
<td id="td" onclick="if(top.body) Len('td')" class="lenDesActiv">Date</td>
I tried the following:
driver.execute_script('document.querySelector("td#tdDos.lenguetaActiva").click();')
driver.execute_script('document.querySelector("td#").click();')
driver.execute_script('document.querySelector("#td").click();')
driver.execute_script('document.querySelector("td#td").click();')
driver.execute_script('document.getElementById("td").click();')
driver.findElement(By.id("td")).click();
document.getElementById("td").addEventListener("click", function(){
alert("hello world");
});
loginelement = driver.find_element(By.ID, 'td')
driver.execute_script("arguments[0].click();", loginelement)

The WebElement with text as Fecha is within a <frame> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://your_website.com/')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[name='body']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td#tdDos.lenguetaDesActiva"))).click()
Using XPATH:
driver.get('https://your_website.com/')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[#name='body']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[#id='tdDos' and #class='lenguetaDesActiva']"))).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:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python

Your issue was an iframe switch to it first.
driver.get("https://webpage.com")
driver.switch_to.frame(driver.find_element_by_xpath("/html/frameset/frameset/frame[2]"))
loginelement = driver.find_element(By.ID, 'td')
driver.execute_script("arguments[0].click();", loginelement)

Try this.
First switch to the frame.
driver.switch_to.frame("body")
wait for the element to be clickable.
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH, '//td[#id="td"]')))
Click on element using JavaScriptExecutor.
driver.execute_script('document.querySelector("td").click()')

Related

Python Selenium: Unable to find xpath to click on element within iframe

I'm using this code but the can't get it to work with the xpath:
browser = webdriver.Chrome(chrome_path)
browser.get("https://planetradio.co.uk/cool-fm")
time.sleep(5)
browser.find_element_by_xpath('//*[#id="notice"]/div[4]/button[2]').click()
The element ACCEPT ALL is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://planetradio.co.uk/cool-fm/')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='ACCEPT ALL']"))).click()
Using XPATH:
driver.get('https://planetradio.co.uk/cool-fm/')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='SP Consent Message']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#title='ACCEPT ALL']"))).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:
References
You can find a couple of relevant discussions in:
Switch to an iframe through Selenium and python
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

Getting id or xpath of parts of a website

I want to use selenium to login in the website https://www.winamax.es/account/login.php?redir=/apuestas-deportivas. The case is that I don't find the xpath/id/text to get te next code running successfully:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
options = webdriver.ChromeOptions()
options.add_argument("window-size=1920,1080")
options.add_argument('--disable-blink-features=AutomationControlled')
driver=webdriver.Chrome(options=options,executable_path=r"chromedriver.exe")
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
WebDriverWait(driver=driver, timeout=15).until(
lambda x: x.execute_script("return document.readyState === 'complete'")
)
upload_field = driver.find_element_by_xpath("//input[#type='email']")
I don't only want the specific xpath for this example, but I prefer a method to obtain the xpath or something similar to get the code working for other parts of the website
<iframe id="iframe-login" data-node="iframe" name="login" scrolling="auto" frameborder="1" style="min-height: 280px; width: 100%;"></iframe>
Your element is in an iframe. Switch to it.
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iframe-login")))
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Full working code.
wait = WebDriverWait(driver, 10)
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iframe-login")))
upload_field = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#type='email']")))
upload_field.send_keys("stuff")
The email element is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#iframe-login")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='email']"))).send_keys("scraper#stackoverflow.com")
Using XPATH:
driver.get("https://www.winamax.es/account/login.php?redir=/apuestas-deportivas")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='iframe-login']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#type='email']"))).send_keys("scraper#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:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
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

How to insert data in frame using selenium

I am using selenium with python I want to insert the username and email inputs containing in frame
Here is the link
I am trying to do this but it's not switching into the modal.
try:
time.sleep(3)
driver.switch_to.frame(driver.find_element_by_id("thanksModal"))
driver.find_element_by_xpath("//input[#type='file']").send_keys("C:\\Users\\user\\Desktop\\Resume.docx")
driver.find_element_by_id('txtName').send_keys(name)
driver.find_element_by_id('txtEmail').send_keys(email)
driver.find_element_by_id('btnSubmit').click()
time.sleep(5)
except:
pass
The elements are within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://www.collabera.com/find-a-job/search-jobs/job-details/248429-python-developer-jobs-jersey-city-nj/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.apply-form")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#txtName"))).send_keys("bhupathi_turaga#stackoverflow.com")
driver.find_element_by_css_selector("input#txtEmail").send_keys("bhupathi_turaga#stackoverflow.com")
Using XPATH:
driver.get("https://www.collabera.com/find-a-job/search-jobs/job-details/248429-python-developer-jobs-jersey-city-nj/")
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#class='apply-form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='txtName']"))).send_keys("bhupathi_turaga#stackoverflow.com")
driver.find_element_by_xpath("//input[#id='txtEmail']").send_keys("bhupathi_turaga#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:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
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

Unable to reliably select element regardless of method using Selenium

I'm trying to select the "Majors" button on [this webpage] (https://www.dukascopy.com/swiss/english/marketwatch/historical/)
I am able to switch into the iframe where this element is contained, but trying to click by using the xpath, id or class name of the element all result in a seemingly random button within the same column being selected. Below is the current code I'm using along with a screenshot of inspection of the button.
majorforexbutton = driver.find_element_by_xpath('/html/body/div[9]/div[1]/div[3]/ul/li[4]')
majorforexbutton.click()
screenshot:
The element with text as Majors field is within a <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://www.dukascopy.com/swiss/english/marketwatch/historical/')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='freeserv.dukascopy.com/2.0/?path=historical_data_feed']")))
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Instrument']"))))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li[data-group='FX_MAJORS'][data-parent='FX']"))).click()
Using XPATH:
driver.get('https://www.dukascopy.com/swiss/english/marketwatch/historical/')
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(#src, 'freeserv.dukascopy.com/2.0/?path=historical_data_feed')]")))
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Instrument']"))))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[text()='Majors']"))).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:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
Try grabbing your element and trying either one of the bottom.
elem = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.ID, ":5x")))
1 of these:
elem.click()
driver.execute_script("arguments[0].click();", elem)

Python/Selenium Click on an element in a ul

I am new to Python/Selenium (< 3 days). I am trying to click on the "grades" item of an unordered list (Please see image). I have tried find_element_by_link_text, find_element_by_css_selector but am unable to find it.
Image from inspect element
Thanks.
As per the HTML it is pretty clear that the desired field is within an <iframe> so you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"frameDetail")))
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#grades")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"frameDetail")))
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#id='grades']")))
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 couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python

Categories

Resources