i want to click on button that means next and it wrote by 'بعدی'
in this page
https://www.tgju.org/profile/price_dollar_rl/history
here is my code
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument("--headless")
browser= webdriver.Firefox(options=options, executable_path="geckodriver.exe")
browser.get('https://www.tgju.org/profile/price_dollar_rl/history');
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
wait = WebDriverWait(browser,20)
from selenium.webdriver.common.by import By
browser.find_element_by_xpath('//*[#id="DataTables_Table_0_next"]')
and I get this error
Unable to locate element: //*[#id="DataTables_Table_0_next"]
but i copy exact id form inspect
thanks
You need to add a wait for element to be present. On this page it looks like it's dynamically loaded.
element = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "DataTables_Table_0_next"))
)
Related
I want to access an website with selenium and than a addblock-window appears, in which i need to click a button for it to disappear.
Eventhough I can find my XPath(//button[#title='Einverstanden'], /html/body/div/div[2]/div[3]/div[1]/button[#title = 'Einverstanden'] or
//button[contains(text(),"Einverstanden")]') in the browser,
I can't find it with my Python script. And i can't seem to find the mistake.
Here is my code.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.get("``https://www.derstandard.at/story/2000134260361/endspiel-vor-gericht-prozess-gegen-boris-becker-startet-in-london``")
driver.maximize_window()
x = driver.find_element(By.XPATH, "//button[#title = 'Einverstanden']")
print(x)
This is the error I'm getting.
The button is enclosed in an iframe in which case, you first need to switch to the iframe and then access the element
This should work:
driver.get("https://www.derstandard.at/consent/tcf/")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//div[contains(#id, 'sp_message_container')]//iframe")))
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button[title='Einverstanden']"))).click()
Wait Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
In case you want to switch to default frame again, you may use this when required:
driver.switch_to.default_content()
I'm trying to login with seleinum automatically. I've used
driver.execute_script
driver.find_element_by_css_selector
driver.find_element_by_xpath
.
from selenium import webdriver
from webdriver_manager import chrome
driver = webdriver.Chrome(chrome.ChromeDriverManager().install())
driver.get("https://naco999.com/")
driver.find_element_by_css_selector('#login_id').send_keys("id")
driver.find_element_by_css_selector("#login_pw").send_keys("pw")
But none of these seems to work. How can I?
There are 3 elements matching #login_id css_selector.
Try using this:
driver.find_element_by_css_selector(".header-one #login_id").send_keys("id")
driver.find_element_by_css_selector(".header-one #login_pw").send_keys("pw")
Also, you should add a wait to send the text when the elements are loaded.
Like this:
from selenium import webdriver
from webdriver_manager import chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(chrome.ChromeDriverManager().install())
wait = WebDriverWait(driver, 20)
driver.get("https://naco999.com/")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".header-one #login_id"))).send_keys("id")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".header-one #login_pw"))).send_keys("pw")
im trying to print all links but i have an error ( ut is not clickable at point (781,748) because another element obscures it
the code updated:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
#driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver = webdriver.Firefox(executable_path='geckodriver')
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
driver.get("https://www.architectes-pour-tous.fr/")
driver.find_element_by_xpath("//button[contains(#class,'decline-button')]").click();
driver.find_element_by_xpath(".//a[#id='pager']").click();
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.image-projet img")))
time.sleep(1)
for img in driver.find_elements_by_css_selector('div.image-projet img'):
print(a.get_attribute('href'))
driver.find_elements_by_css_selector('button.agree-button').click()
pager = driver.find_element_by_xpath('//*[#id="pager"]')
actions.move_to_element(pager).build().perform()
time.sleep(0.5)
pager.click()
You have to accept/decline the cookies before accessing any element on the page.
driver.find_element_by_xpath("//button[contains(#class,'decline-button')]").click();
driver.find_element_by_xpath(".//a[#id='pager']").click();
The element you trying to access is initially out of the visible screen so you have to scroll to it before clicking it.
Also possibly you will have to close the accept cookies pop-up prior to clicking this element.
Also I'm quite sure you are getting no links with
for a in driver.find_elements_by_xpath('.//a'):
print(a.get_attribute('href'))
since you trying to do that before the page is loaded.
Also, if you are trying to get the search results links you have to use another locator.
So would suggest to change your code as following:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import time
#driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver = webdriver.Firefox(executable_path='geckodriver')
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
driver.get("https://www.architectes-pour-tous.fr/")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.image-projet img")))
time.sleep(1)
for img in driver.find_elements_by_css_selector('div.image-projet img'):
print(a.get_attribute('href'))
driver.find_element_by_css_selector('button.agree-button').click()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
pager = driver.find_element_by_xpath('//*[#id="pager"]')
actions.move_to_element(pager).perform()
time.sleep(0.5)
pager.click()
The task is to click on the element, go to a new window and get the url of the new window. But instead of url "about: blank" is displayed. How to fix it?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox(executable_path=r'C:\SeleFire\geckodriver.exe')
url = driver.get('http://the-internet.herokuapp.com/windows')
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, 'a[href="/windows/new"]'))).click()
driver.switch_to.window(driver.window_handles[1])
print(driver.current_url)
driver.quit()
2 possible problems here:
Wait for element visibility, not just presence
Small sleep after the click
Try this:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox(executable_path=r'C:\SeleFire\geckodriver.exe')
url = driver.get('http://the-internet.herokuapp.com/windows')
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'a[href="/windows/new"]'))).click()
time.sleep(1)
driver.switch_to.window(driver.window_handles[1])
print(driver.current_url)
driver.quit()
I am trying to press this "New Search" button. It appears on the top of the screen after entering a search on http://www.lexisnexis.com/hottopics/lnacademic/
I have looked at the Xpath and Unique Selector.
What I have tried:
browser.find_element_by_css_selector('#restoreButtons > a:nth-child(3)').click()
browser.find_element_by_xpath(id('restoreButtons')/x:a[3])
browser.find_element_by_xpath(/x:a[3])
For all three I get an "unable to locate element error"
To build on alecxes answer,
once you're inside the iframe, you can find the clickable using it's xpath:
new_search_xpath = '/html/body/div[2]/table/tbody/tr[2]/td[2]/div[1]/table/tbody/tr/td/span/a[3]'
new_search = driver.find_element(By.XPATH, new_search_xpath)
new_search.click()
You should consider installing firebug firefox addon to grab Xpaths:
https://addons.mozilla.org/en-US/firefox/addon/firebug/
This is because the element is inside an iframe. You have to be in the context to search elements inside. Use .switch_to.frame():
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, 10)
frame = wait.until(EC.presence_of_element_located((By.ID, "mainFrame")))
driver.switch_to.frame(frame)
FYI, here is the complete working code:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://www.lexisnexis.com/hottopics/lnacademic/')
actions = ActionChains(driver)
wait = WebDriverWait(driver, 10)
frame = wait.until(EC.presence_of_element_located((By.ID, "mainFrame")))
driver.switch_to.frame(frame)
driver.find_element_by_id("terms").send_keys("Test")
driver.find_element_by_id("srchButt").click()