I am trying to find the element with the link text, I am using following code
handle = driver.window_handles
#handle for windows
driver.switch_to.window(handle[1])
#switching to new window
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Followers ")))
And I am getting following traceback
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Followers ")))
File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
raise TimeoutException(message)
TimeoutException: Message: ''
HTML of the element I am trying to select is
Followers <span class="profile_count">43,799</span>
How can I solve this problem??
If you use By.LINK_TEXT, there should be a link with exactly that text: Followers, but you have Followers 43,799.
In your case, you should use By.PARTIAL_LINK_TEXT instead:
wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, 'Followers')))
UPDATE Here's working example:
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.Chrome() # CHANGEME
driver.get('http://www.quora.com/Kevin-Rose')
element = WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Followers"))
)
element.click()
Cheers
from selenium import webdrivercode
from selenium.webdriver.common.by import By
import selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http://somedomain/url_that_delays_loading")
#Follow below syntax
element = WebDriverWait(driver, 10)
element.until(EC.presence_of_element_located((By.ID, '--webElement--')))
Related
Below I have given my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
browser = webdriver.Firefox()
browser.maximize_window()
browser.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html")
browser.implicitly_wait(5)
print(browser.title)
source_element = browser.find_element(By.XPATH, "//*[#id='DHTMLgoodies_dragableElement5']")
target_element = browser.find_element(By.XPATH,"//*[#id='box106']")
actions = ActionChains(browser)
actions.drag_and_drop(source_element,target_element).perform()
My output:-
/home/halovivek/PycharmProjects/pythonProject/venv/bin/python /home/halovivek/PycharmProjects/pythonProject/draganddrop.py
Demo 2: Drag and drop
Traceback (most recent call last):
File "/home/halovivek/PycharmProjects/pythonProject/draganddrop.py", line 21, in <module>
actions.drag_and_drop(source_element,target_element).perform()
File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/common/action_chains.py", line 75, in perform
self.w3c_actions.perform()
File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/common/actions/action_builder.py", line 77, in perform
self.driver.execute(Command.W3C_ACTIONS, enc)
File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
self.error_handler.check_response(response)
File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: TypeError: rect is undefined
Stacktrace:
element.getInViewCentrePoint#chrome://remote/content/marionette/element.js:1185:5
getElementCenter#chrome://remote/content/marionette/action.js:1497:22
dispatchPointerMove/<#chrome://remote/content/marionette/action.js:1378:34
dispatchPointerMove#chrome://remote/content/marionette/action.js:1374:10
toEvents/<#chrome://remote/content/marionette/action.js:1145:16
action.dispatchTickActions#chrome://remote/content/marionette/action.js:1055:35
action.dispatch/chainEvents<#chrome://remote/content/marionette/action.js:1023:20
action.dispatch#chrome://remote/content/marionette/action.js:1029:5
performActions#chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:459:18
receiveMessage#chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:144:31
Process finished with exit code 1
I could not able to drag and drop it.
How to do the Perform action? I have tried many ways but I could not able to find a correct solution.
To drag_and_drop() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using XPATH:
driver.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html")
drag = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#id, 'box') and text()='Rome']")))
drop = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#id, 'box') and text()='South Korea']")))
ActionChains(driver).drag_and_drop(drag, drop).perform()
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:
This works for me
driver.maximize_window()
driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')
time.sleep(5)
drg = driver.find_element(By.XPATH, "//*[#id='dropContent']//div[#class='dragableBox' and #id='box5']")
drp = driver.find_element(By.XPATH, "//*[#id='countries']//div[#class='dragableBoxRight' and #id='box105']")
ActionChains(driver).drag_and_drop(drg, drp).perform()
time.sleep(5)
driver.quit()
Snapshot
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Firefox()
driver.maximize_window()
driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')
time.sleep(5)
driver.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html")
drag = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#id, 'box') and text()='Rome']")))
drop = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#id, 'box') and text()='South Korea']")))
ActionChains(driver).drag_and_drop(drag, drop).perform()
'''drg = driver.find_element(By.XPATH, "//*[#id='dropContent']//div[#class='dragableBox' and #id='box5']")
drp = driver.find_element(By.XPATH, "//*[#id='countries']//div[#class='dragableBoxRight' and #id='box105']")
ActionChains(driver).drag_and_drop(drg, drp).perform()
time.sleep(5)'''
#driver.quit()
This is the modified code.
Now its working. Thank you so much for the help.
When trying to execute the following code:
driver = webdriver.Firefox()
driver.get("https://www.flashscore.co.uk/tennis/")
cookie_button = driver.find_element_by_id("onetrust-accept-btn-handler")
cookie_button.click()
I'm getting this exception:
Exception has occurred: NoSuchElementException
Message: Unable to locate element: [id="onetrust-accept-btn-handler"]
When I look through the HTML on the page I can see the following line which is the button I want to click on:
<button id="onetrust-accept-btn-handler">I Accept</button>
Picture of site below:
Picture of the HTML "Inspector":
(apologies not sure how to copy this as text)
Why can't Selenium locate the element?
You need to use ExplicitWait
The below code works for me :
executablePath = r'C:\geckodriver.exe'
options = webdriver.FirefoxOptions()
driver = webdriver.Firefox(executable_path = executablePath, options=options)
driver.maximize_window()
driver.get("https://www.flashscore.co.uk/tennis/")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[#id='onetrust-accept-btn-handler']"))).click()
print('Operation successful')
Imports :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I'm trying to scrape a website with Selenium. After I got past the login, I try delaying, finding an element, and printing it's text with the .text property. Below is my code:
from login_info import *
from selenium import webdriver
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(executable_path="/users/aliallam/Desktop/scraper test/chromedriver")
url = 'https://results.decisiondeskhq.com/2020/primary/colorado/president'
driver.get(url)
# Bypass loading page and click login link
delay = 10 # seconds
login_button = WebDriverWait(driver, delay).until(
EC.presence_of_element_located((By.ID, 'login-text-btn')))
driver.execute_script("arguments[0].click();", login_button)
##
# Login
email = driver.find_element_by_xpath('//*[#id="content"]/div/div/div/form/div[2]/div/div[1]/div/input')
email.send_keys(login_email)
password = driver.find_element_by_xpath('//*[#id="content"]/div/div/div/form/div[3]/div/div[1]/div/input')
password.send_keys(login_pass)
login_button_actual = driver.find_element_by_xpath('//*[#id="content"]/div/div/div/form/button/div')
login_button_actual.click()
##
results_table = WebDriverWait(driver,delay).until(
EC.presence_of_element_located((By.ID, 'overview-info'))
)
print(results_table.text())
#results_table = driver.find_element_by_id('overview-info')
#print(results_table.text)
This is the error I get:
Traceback (most recent call last):
File "/Users/aliallam/Desktop/scraper test/main.py", line 37, in <module>
print(results_table.text())
TypeError: 'str' object is not callable
Help is greatly appreciated!
It's not possible to call a string, but it's possible to access the text property of results_table object.
Instead of:
results_table.text()
try accessing the text property:
results_table.text
Please refer below answer, your xpath is also wrong for a table and you need print results_table.text to print your table.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.get('https://results.decisiondeskhq.com/2020/primary/colorado/president')
driver.maximize_window()
delay = 10
login_button = WebDriverWait(driver, delay).until(
EC.presence_of_element_located((By.ID, 'login-text-btn')))
driver.execute_script("arguments[0].click();", login_button)
##
# Login
email = driver.find_element_by_xpath('//*[#id="content"]/div/div/div/form/div[2]/div/div[1]/div/input')
email.send_keys("username")
password = driver.find_element_by_xpath('//*[#id="content"]/div/div/div/form/div[3]/div/div[1]/div/input')
password.send_keys("password")
login_button_actual = driver.find_element_by_xpath("//div[contains(text(),'Log In')]")
actionChains = ActionChains(driver)
actionChains.move_to_element(login_button_actual).click().perform()
results_table = WebDriverWait(driver,delay).until(EC.presence_of_element_located(By.XPATH, "(//table[#class='v-datatable v-table theme--light''])[1]"))
print results_table.text
I have code that is supposed to log into etsy after a certain amount of time. This code has not had any changes and used to work perfectly on an old machine that I had. Now however when I run this on another windows 10 machine, I get a time out exception.
Code:
import selenium
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.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from datetime import datetime
import time
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome()
browser.get("https://www.etsy.com/shop/FidoandFifi?ref=l2-shopheader-name") #navigates to hoshiikins.com
print("Navigating to website...")
browser.find_element_by_id("sign-in").click()
wait = WebDriverWait(browser, 10)
wait.until(EC.element_to_be_clickable((By.ID,"username-existing")))
username = browser.find_element_by_id("username-existing")
password = browser.find_element_by_id("password-existing")
username.click()
username = browser.find_element_by_id("username-existing")
It gets to the wait.until line then it times out with this message:
> Navigating to website...
Traceback (most recent call last):
File "EstyBot.py", line 183, in <module>
main()
File "EstyBot.py", line 180, in main
get_item(item)
File "EstyBot.py", line 61, in get_item
wait.until(EC.element_to_be_clickable((By.ID,"username-existing")))
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
I have tried using sleep, but then I get the error that the element is not visible.
I have tried by XPATH and tried EC as element to be visible to only hit the same error message as below.
Seems the desired locators have changed and you can use the following solution:
Code Block:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
browser.get("https://www.etsy.com/shop/FidoandFifi?ref=l2-shopheader-name") #navigates to hoshiikins.com
print("Navigating to website...")
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#sign-in"))).click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input.input.input-large#join_neu_email_field"))).send_keys("Taryn#Antoinette.Raines")
browser.find_element_by_css_selector("input.input.input-large#join_neu_password_field").send_keys("Taryn_Antoinette_Raines")
Browser Snapshot:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chrome_path=r"C:\Users\Priyanshu\Downloads\Compressed\chromedriver_win32\chromedriver.exe"
driver=webdriver.Chrome(chrome_path)
driver.get("https://www.flipkart.com/?")
search = driver.find_element_by_name('q')
search.send_keys("laptop")
search.send_keys(Keys.RETURN)
driver.find_element_by_xpath(""" //*[#id="container"]/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2]""").click()
I am getting no such element present in the last line of code.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chrome_path=r"C:\Users\Priyanshu\Downloads\Compressed\chromedriver_win32\chromedriver.exe"
driver=webdriver.Chrome(chrome_path)
driver.get("https://www.flipkart.com/search?q=laptop&otracker=start&as-show=off&as=off")
driver.find_element_by_xpath(""" //*[#id="container"]/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2]""").click()
If I am doing like this its working fine.
The element is not immediately available, wait for it to be present first:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
search = wait.until(EC.presence_of_element_located((By.NAME, 'q')))
search.send_keys("laptop")
search.send_keys(Keys.RETURN)
element = wait.until(EC.presence_of_element_located(By.XPATH, '//*[#id="container"]/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2]'))
element.click()
Note that, assuming you want to get to the "Popularity" menu header, why don't simplify the XPath expression and use the element's text:
//li[. = "Popularity"]