I tried with XPath but selenium can't click this image/button.
from undetected_chromedriver.v2 import Chrome
def test():
driver = Chrome()
driver.get('https://bandit.camp')
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[1]/div/main/div/div/div/div/div[5]/div/div[2]/div/div[3]/div"))).click()
if __name__ == "__main__":
test()
Try the below one, I checked it, and it is working fine, while clicking on the link it is opening a separate window for login.
free_case = driver.find_element(By.XPATH, ".//p[contains(text(),'Open your free')]")
driver.execute_script("arguments[0].scrollIntoView(true)", free_case)
time.sleep(1)
driver.execute_script("arguments[0].click();", free_case)
First you need to wait for presence of that element.
Then you need to scroll the page down to make that element visible since initially this element is out of the visible screen so you can't click it.
Now you can click it and it works.
The code below is working:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument('--disable-notifications')
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://bandit.camp/"
driver.get(url)
element = wait.until(EC.presence_of_element_located((By.XPATH, "//div[contains(.,'daily case')][contains(#class,'v-responsive__content')]")))
element.location_once_scrolled_into_view
time.sleep(0.3)
element.click()
Related
I'm using selenium to get to YouTube and write something on the search bar and then press the button or press the enter key.
Both clicking or pressing a key does sometimes work, but sometimes it does not.
I tried to wait with WebDriverWait, and I even changed the waiting time from 10 to 20 seconds, but it didn't make any difference.
And if I add anything (like printing the new page title), it only shows me the first page title and not the title after the search.
Here is my code and what I tried:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def get_driver():
firefox_options = Options()
# firefox_options.add_argument("--headless")
driver = webdriver.Firefox(executable_path=r"C:\Program Files\Mozilla Firefox\geckodriver.exe", options=firefox_options)
driver.implicitly_wait(9)
return driver
driver = get_driver()
driver.get('https://www.youtube.com/')
search = driver.find_element(By.XPATH, '//input[#id="search"]')
search.send_keys("python")
# search.send_keys(Keys.ENTER) #using the enter key # If I add nothing after this line it work
# searchbutton = driver.find_element(By.XPATH,'//*[#id="search-icon-legacy"]') # This also dose doesn't work
# searchbutton.click() # using the click method() #also dose not work
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="search-icon-legacy"]'))).click() # Sometimes work
# driver.implicitly_wait(10)
# print(driver.title) # This show me only the title of the first page not the one after the search
Is it because I use the Firefox webdriver (should I change to Chrome)?
Or is it because of my internet connection?
To make this working you need to click the search field input first, then add a short delay and then send the Keys.ENTER or click search-icon-legacy element.
So, this is not your fault, this is how YouTube webpage works. You may even call it a kind of bug. But since this webpage it built for human users it works good since human will never click on the input field and insert the search value there within zero time.
Anyway, the 2 following codes are working:
First.
import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument('--disable-notifications')
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://www.youtube.com/"
driver.get(url)
search = wait.until(EC.element_to_be_clickable((By.XPATH, '//input[#id="search"]')))
search.click()
time.sleep(0.2)
search.send_keys("python")
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="search-icon-legacy"]'))).click()
Second.
import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
options.add_argument('--disable-notifications')
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://www.youtube.com/"
driver.get(url)
search = wait.until(EC.element_to_be_clickable((By.XPATH, '//input[#id="search"]')))
search.click()
time.sleep(0.2)
search.send_keys("python" + Keys.ENTER)
I'm having a strange issue with Python Selenium when trying to click on the next page link in Curry's PC World Televisions page. See the next button on (https://www.currys.co.uk/tv-and-audio/televisions/tvs)
I am testing in non-headless mode with chrome, so i can see the outline of the next button being highlighted, when actioning the click, but it is not navigating to the next page url as expected.
Here is what i have tried on the page above:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
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.common.exceptions import NoSuchElementException
import time
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.set_window_size(1920, 1080)
driver.maximize_window()
driver.get('https://www.currys.co.uk/tv-and-audio/televisions/tvs')
next_page_link = WebDriverWait(driver, 10).until(
ec.element_to_be_clickable((By.CSS_SELECTOR,
'ul.pagination '
'li.pagination-arrrow-next a.page-next')))
next_page_link.click()
time.sleep(10)
I have also tried using execute script, (which does not work either):
driver.execute_script('arguments[0].click()', next_page_link)
I am able to extract the href from the link element (so i know its the right element):
href = next_page_link.get_attribute('href')
print("href is: {}".format(href))
# href is: https://www.currys.co.uk/tv-and-audio/televisions/tvs?start=20&sz=20
I'm a bit lost as to what the issue might be here, would appreciate any help!
I see the following issues here:
You have to close the accept cookies pop-up.
You need to scroll the page down to the pager elements.
Selenium indeed doesn't make the page to go to the next page BUT even when I click the Next button manually or even any other pagination button there manually the page is not changed. So, the problem is with the web page itself, not with Selenium.
This is the code I used:
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = 'https://www.currys.co.uk/tv-and-audio/televisions/tvs'
driver.get(url)
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
next_page_link = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'ul.pagination li.pagination-arrrow-next a.page-next')))
next_page_link.location_once_scrolled_into_view
time.sleep(1)
next_page_link.click()
I just need to click the load more button once to reveal a bunch more information so that I can scrape more HTML than what is loaded.
The following "should" go to github.com/topics and find the one and only button element and click it one time.
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Edge()
driver.get("https://github.com/topics")
time.sleep(5)
btn = driver.find_element(By.TAG_NAME, "button")
btn.click()
time.sleep(3)
driver.quit()
I'm told Message: element not interactable so I'm obviously doing something wrong but I'm not sure what.
use
btn = driver.findElementsByXPath("//button[contains(text(),'Load more')]");
You are not finding the right element. This is the reason why it is not "interactable"
There are several issues with your code:
The "Load more" button is initially out of the view, so you have to scroll the page in order to click it.
Your locator is bad.
You need to wait for elements to appear on the page before accessing them. WebDriverWait expected_conditions explicit waits should be used for that, not hardcoded sleeps.
The following code works, it scrolls the page and clicks "Load more" 1 time.
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)
url = "https://github.com/topics"
driver.get(url)
load_more = wait.until(EC.presence_of_element_located((By.XPATH, "//button[contains(.,'Load more')]")))
load_more.location_once_scrolled_into_view
time.sleep(1)
load_more.click()
UPD
You can simply modify the above code to make it clicking Load more button while it presented.
I implemented this with infinite while loop making a break if Load more button not found. This code works.
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 5)
url = "https://github.com/topics"
driver.get(url)
while True:
try:
load_more = wait.until(EC.presence_of_element_located((By.XPATH, "//button[contains(.,'Load more')]")))
load_more.location_once_scrolled_into_view
time.sleep(1)
load_more.click()
except:
break
I want to change or at least make any small effect in the korean custom website but it seems they are not accessible by my code! Maybe they are in the internal iframe or not, I don't know. I want to change dropdown, write something in textbox and click search button, but I cannot.
May anyone help me?
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
webdriver_service = Service('C:\Webdriver\chromedriver.exe')
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
url = 'http://www.kita.org/kStat/byCom_AllCount.do'
browser.get(url)
time.sleep(5)
select = Select(WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/table/tbody/tr/td/table/tbody/tr/td/form/table/tbody/tr/td/table[3]/tbody/tr/td/table/tbody/tr/td/table/tbody/tr[2]/td[2]/select')))) # select dropdown
select.select_by_index(1)
browser.find_element(By.XPATH,'/html/body/table/tbody/tr/td/table/tbody/tr/td/form/table/tbody/tr/td/table[2]/tbody/tr/td[2]/table/tbody/tr/td[2]/a/img').click() #click seach Button
time.sleep(5)
There is an iframe there.
You need to switch to it first in order to access elements in it.
Also, you should improve your locators.
And insert some text into the item input.
The following code works
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = "http://www.kita.org/kStat/byCom_AllCount.do"
driver.get(url)
wait = WebDriverWait(driver, 20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"iframe_stat")))
select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='cond_choosefield']"))))
select.select_by_index(1)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='cond_prdt_cd']"))).send_keys("kuku")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href*='searchForm']"))).click()
I have a url login script that was working fine for a few days but then quit, that used the chromium webdriver
driver = webdriver.Chrome('/snap/bin/chromium.chromedriver', options=option)
I can access the URL using Firefox or Chromium without my script (just normal). Or Firefox in a script.
But using Chromium in a script, it clicks the login button and just hangs, that eventually leads to a timeout
selenium.common.exceptions.TimeoutException: Message:
If I open a new tab and try to login without the script but manually, it still hangs. A login is impossible (on my target site) within a launched browser from selenium, only.
#!/usr/bin/python3
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from seleniumbase import BaseCase
import time
import random
user_name = 'user'
password = 'password'
list = [
]
minptime = 4
maxptime = 24
list_length = len(list)
print('Array length ', list_length)
class MyMeClass(BaseCase):
def method_a():
option = webdriver.ChromeOptions()
option.add_argument('--disable-notifications')
option.add_argument("--mute-audio")
driver = webdriver.Chrome('/snap/bin/chromium.chromedriver', options=option)
driver.get("https://me.com/")
print(driver.title)
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.login-btn.btn-shadow#login-fake-btn[data-testid='login-fake-btn']"))).click()
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/form/input[1]').send_keys(user_name)
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/form/input[2]').send_keys(password)
time.sleep(5)
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/form/button').click() #button click in question
time.sleep(8)
driver.get(url)
print(driver.current_url)
return driver
driver = MyMeClass.method_a()
button I am accessing
How do I use/unblock the use of this login button in chromium in a script?
Try below code:
with contains
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Log in')]"))).click()
Class Name
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn-login btn-shadow']"))).click()
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Working Solution:
driver.get(" your url ")
wait = WebDriverWait(driver,30)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#id='login-fake-btn']")))
print element.text
element.click()
wait = WebDriverWait(driver,30)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='email']"))).send_keys("Test")
wait = WebDriverWait(driver,30)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='password']"))).send_keys("Test")
element1 = wait.until(EC.presence_of_element_located((By.XPATH, "//div[#id='login-overlay']//div//form//button")))
element1.click()
output :