Problem with location of link in Selenium/python - python

I have a problem. I want to click on Zadanie 1 using Selenium here:
https://buggy-testingcup.pgs-soft.com/
I tried xpath, partial links, link text and so on, but all the time I have error
Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/div/div[2]/div[1]/a/h2"}
Message: no such element: Unable to locate element: {"method":"partial link text","selector":"Zadanie 1"}
How can I locate this link?
<a href="/task_1">
<h2>Zadanie 1</h2>
</a>
It's weird for me, because when I run code of #0m3r it works. but when i paste it to my project i get errors.
import unittest
import time
import HtmlTestRunner
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 as wait
from selenium.webdriver.support import expected_conditions as EC
from ASTA.Pages.HomePage import ASTA_homepage
class test_task1(unittest.TestCase):
#classmethod
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(10)
self.driver.maximize_window()
def test_open_task(self):
driver = self.driver
driver.get("https://asta.pgs-soft.com/")
open_app = ASTA_homepage(driver)
open_app.open_buggy_app()
time.sleep(3)
open_app.open_task1()
#classmethod
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='C:/Users/Warpath/PycharmProjects/allegro_tests/venv/ASTA'))
class ASTA_homepage():
def __init__(self, driver):
self.driver = driver
def open_buggy_app(self):
self.driver.find_element_by_partial_link_text("Buggy app").click()
def open_task1(self):
link = self.driver.find_element_by_link_text("Zadanie 1")
link.click()

Find it by link text
Example
import time
from selenium import webdriver
chrome_browser = webdriver.Chrome()
url = "https://buggy-testingcup.pgs-soft.com/"
chrome_browser.get(url)
time.sleep(3)
link = chrome_browser.find_element_by_link_text("Zadanie 1")
link.click()

Try using a Selenium method other than find_element_by_xpath. You can find elements of an xpath, jspath, inner HTML, etc
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
These are the available methods Selenium allows.
I have a form filling application where every element can be found by name but one of them needs to be found exclusively by xpath for some reason.

I was able to click on that button using this below. Also, it's always good practice to use waits when attempting to find or interact with elements.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
# You will need to use the path for your driver
driver = webdriver.Chrome('/Users/Shared/Drivers/chromedriver')
driver.get('https://buggy-testingcup.pgs-soft.com/')
button = wait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '[href=\"\/task_1\"]')))
button.click()
You can find more information regarding the use of waits here: https://selenium-python.readthedocs.io/waits.html

Related

Using Python , Selenium webdriver click() method is not working

I'm using selenium 4 with python to automate webpage.
Click() method is not working.
Able to click a button using submit() method only if the path has type=submit, but couldnt able to select a link using click method. Please help if anyone knows alternative option for click() method to click a link.
The following is the code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
s = Service("C:/Program Files/webdriver/Chrome Driver/chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.get("https://www.sololearn.com/users/login")
time.sleep(2)
#driver.find_element(By.CLASS_NAME, "sl-login-login-form").send_keys(Keys.ENTER)
#---click here fails--------
driver.find_element(By.CLASS_NAME, "sl-login-login-form").click()
#---click here fails
#driver.find_element(By.CLASS_NAME, "sl-login-login-form").submit()
#login.click()
time.sleep(5)
driver.quit()
**Also i have tried with the below code but still it is not working:**
driver.find_element(By.CLASS_NAME, "sl-login-login-form").send_keys(Keys.ENTER)
driver.find_element(By.CLASS_NAME, "sl-login-login-form").submit()
login.click()
Try the below
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "CLASSNAME")))
element.click();
OR
element = driver.find_element(By.CLASS_NAME, "className")
driver.execute_script("arguments[0].click();", element)
Do not forget to import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import org.openqa.selenium.JavascriptExecutor

Crawling JavaScript site with selenium (python) returns error: Message: no such element: Unable to locate element:

I am new to python and webcrawling in general. I started with BeautifulSoup but quickly learned that sites that use JavaScript cant be crawled with bs4, so I started using selenium. Selenium, however, also returns an error and cant find the elements (search box) I am trying to scrape. So far I have also learned, that the page I am trying to crawl probably uses Angular, which somehow hides the elements I am looking for. Is there a way I could still use selenium or another package to enter search queries and crawl the site?
Any element I try to find cant be found, ive also tried finding them via xpath or name with out luck. I believe anything inside <app-root></app-root> cant be found simply with selenium.
Here is my code so far
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
import time
import sys
chrome_driver_path = "path"
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--no-sandbox')
webdriver = webdriver.Chrome(
executable_path=chrome_driver_path,
options=chrome_options
)
useBaseURL = "https://ec.europa.eu/info/funding-tenders/opportunities/portal/screen/home"
with webdriver as driver:
# timeout
wait = WebDriverWait(driver, 10)
driver.get(useBaseURL)
searchbox = driver.find_element_by_class_name("ng-tns-c6-0 ui-inputtext ui-widget ui-state-default ui-corner-all ui-autocomplete-input ng-star-inserted")
driver.close()
The following sends keys to that element. Your error was the usage of a compounded class name as a class name. I also added the next click.
driver.get(useBaseURL)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, " p-autocomplete > span > input"))).send_keys("AAA")
driver.find_element_by_css_selector('button.btn.btn-accent.btn-search').click()
Import
from selenium.webdriver.support import expected_conditions as EC

Python 2.7 Selenium No Such Element on Website

I'm trying to do some webscraping from a betting website:
As part of the process, I have to click on the different buttons under the "Favourites" section on the left side to select different competitions.
Let's take the ENG Premier League button as example. I identified the button as:
(source: 666kb.com)
The XPath is: //*[#id="SportMenuF"]/div[3] and the ID is 91.
My code for clicking on the button is as follows:
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
chrome_path = "C:\Python27\Scripts\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("URL Removed")
content = driver.find_element_by_xpath('//*[#id="SportMenuF"]/div[3]')
content.click()
Unfortunately, I always get this error message when I run the script:
"no such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="SportMenuF"]/div[3]"}"
I have tried different identifiers such as CCS Selector, ID and, as shown in the example above, the Xpath. I tried using waits and explicit conditions, too. None of this has worked.
I also attempted scraping some values from the website without any success:
from selenium import webdriver
from selenium.webdriver.common.by import By
chrome_path = "C:\Python27\Scripts\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("URL removed")
content = driver.find_elements_by_class_name('price-val')
for entry in content:
print entry.text
Same problem, nothing shows up.
The website embeddes an iframe from a different website. Could this be the cause of my problems? I tried scraping directly from the iframe URL, too, which didn't work, either.
I would appreciate any suggestions.
Sometimes elements are either hiding behind an iframe, or they haven't loaded yet
For the iframe check, try:
driver.switch_to.frame(0)
For the wait check, try:
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
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '-put the x-path here-')))

why is this code showing NoSuchElementException error? I checked Chrome DOM my XPATH able to find the destinated tag

why is this code showing NoSuchElementException error? I checked Chrome DOM my XPATH able to find the destinated tag.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
class Firefox():
def test(self):
base_url='https://oakliquorcabinet.com/'
driver = webdriver.Chrome(executable_path=r'C:\Users\Vicky\Downloads\chromedriver')
driver.get(base_url)
search=driver.find_element(By.XPATH,'//div[#class="box-footer"]/button[2]')
search.click()
ff=Firefox()
ff.test()
Selenium by default waits for the DOM to load and tries to find the element. But, the confirmation pop up becomes visible after some time the main page is loaded.
Use explicit wait to fix this issue.
add these imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
change line in script:
search = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, '//div[#class="box-footer"]/button[2]')))

Find button's Xpath in webapps [Selenium]

I'm using selenium to browse this page:
https://webapps.cityofchicago.org/activegcWeb/
But I can't find how move to any page, with chrome I get this Xpath for the 'next' button:
'//*[#id="id2"]/a[3]'
I'm using this code:
url = 'https://webapps.cityofchicago.org/activegcWeb/'
driver_1 = webdriver.Firefox()
driver_1.get(url)
content = driver_1.page_source
next_button_xpath = '//*[#id="id2"]/a[3]'
button = driver_1.find_element_by_xpath(next_button_xpath)
button.click()
But I got this error:
'selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//*[#id=\"id2\"]/a[3]"}'
With XPath locator "//a[contains(#href, 'headerPaginator:next')]" and then just click.
Just find the next button by the link text:
driver.find_element_by_link_text(">").click()
Complete working code (including the window maximizing and the wait):
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
url = 'https://webapps.cityofchicago.org/activegcWeb/'
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.maximize_window()
driver.get(url)
# click next
wait.until(EC.visibility_of_element_located((By.LINK_TEXT, ">"))).click()

Categories