unable to locate element by XPATH on Tripadvisor - python

I don't know how to click on this image, I tried many things but it doesn't work. That says the XPATH is wrong.
what I want to click :
my code :
# TripAdvisor
driver.get("https://www.tripadvisor.fr/")
time.sleep(0.5)
driver.implicitly_wait(3)
driver.find_element(By.XPATH, '//*[#id="onetrust-reject-all-handler"]').click()
time.sleep(1.5)
driver.implicitly_wait(3)
if choices == "r":
driver.find_element(By.CSS_SELECTOR, '#lithium-root > main > div.cBOoN > div.rGQXC._T.ivvQp > div > div > div:nth-child(4) > a').click()
driver.implicitly_wait(1)
driver.find_element(By.XPATH, '/html/body/div[3]/div/form/input[1]').send_keys(city)
time.sleep(0.75)
driver.implicitly_wait(2)
driver.find_element(By.XPATH, '//*[#id="typeahead_results"]/a[1]').click()
driver.implicitly_wait(2)
try:
driver.find_element(By.XPATH, '//*[#id="onetrust-reject-all-handler"]').click()
except:
pass
time.sleep(2)
driver.find_element(By.XPATH, '//*[#id="component_48"]/div/div[2]/div[2]/div[3]/div/label').click()
time.sleep(1)
driver.implicitly_wait(2)
driver.find_element(By.XPATH, '//*[#id="component_36"]/div[1]/div[1]/div/span[2]/span').click()
time.sleep(1)
driver.implicitly_wait(2)
driver.find_element(By.XPATH, '//*[#id="component_48"]/div/div[2]/div[2]/div[1]/div/label').click()
driver.find_element(By.XPATH, '//*[#id="component_48"]/div/div[2]/div[2]/div[2]/div/label').click()
time.sleep(2)
driver.implicitly_wait(2)
driver.find_element(By.XPATH, '//*[#id="component_2"]/div/div[1]/div/span/div[1]/div[1]/span[2]/a').click()
I also tried with coordinates but that don't work.
Thank you for anyone who helps me.

You need to improve your locators and use WebDriverWait expected_conditions explicit waits instead of driver.implicitly_wait.
the following 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, 10)
url = "https://www.tripadvisor.fr/"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#data-test-target='nav-links']//a[contains(#href,'Restaurants')]"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[data-test-attribute*='RESTAURANTS'] input[type='search']"))).send_keys("Paris")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#typeahead_results a[href*='Restaurants']"))).click()
time.sleep(1)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='checkbox_5']"))).click()
time.sleep(4)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Supprimer tous les filtres')]"))).click()
time.sleep(2)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='checkbox_3']"))).click()
time.sleep(2)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='checkbox_4']"))).click()
time.sleep(2)
wait.until(EC.presence_of_element_located((By.XPATH, "//a[contains(#href,'Review-')]//div[contains(#class,'carousel')]//span[#class]"))).click()

instead of digging through divs, you could try looping through elements that match a more specific xpath, like this
//a[contains(#href,"html")]//descendant::img[contains(#src,"https://media-cdn")]

Related

Choosing the video quality by using selenium in Trovo

I'm having a problem selecting a quality resolution from a video on Trovo
Please help me switch to 480P. Can't find the info. Thanks
from selenium.common import NoSuchElementException
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--mute-audio")
driver = webdriver.Chrome(executable_path='chromedriver', options=options)
driver.maximize_window()
driver.implicitly_wait(20)
driver.get("https://trovo.live/")
time.sleep(5)
driver.find_element(By.XPATH,
"""/html/body/div[1]/div/div/div[1]/div[2]/section[1]/div/section/div[1]/div/div/div[1]/div/div[1]/div/div[2]/div[2]""").click()
time.sleep(5)
def check_exists_by_xpath(xpath):
try:
driver.find_element(By.XPATH,
"""//*[#id="live-fullscreen"]/div[1]/div/div[4]/div[3]/section/div/button[2]""")
except NoSuchElementException:
return False
return True
if check_exists_by_xpath(True):
driver.find_element(By.XPATH,
"""//*[#id="live-fullscreen"]/div[1]/div/div[4]/div[3]/section/div/button[2]""").click()
time.sleep(3)
driver.find_element(By.XPATH,
'//div[#class="resize-wrap squad-player size0 default-layout"]').click()
time.sleep(0.5)
driver.find_element(By.XPATH,
'//section[#class="v-clarity-switcher"]').click()
time.sleep(10)
if driver:
driver.close()
driver.quit()
I stopped at this step
<div class="vcp-clarityswitcher"><section data-v-bdceacc0="" class="v-clarity-switcher"><svg data-v-bdceacc0="" aria-hidden="true" class="svg-icon clarity-icon icon-360P 360P"><use xlink:href="#icon-360P"></use></svg> <!----></section></div>

Why getting this Error selenium.common.exceptions.StaleElementReferenceException:

I know already upload answer to this same question but I try them they are not working for me because there is also some some update in selenium code too.
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=108.0.5359.95)
When trying to send my searching keyword in this input with label "Skills Search" in advance searching pop-pup form.
Here is the URL: https://www.upwork.com/nx/jobs/search/modals/advanced-search?sort=recency&pageTitle=Advanced%20Search&_navType=modal&_modalInfo=%5B%7B%22navType%22%3A%22modal%22,%22title%22%3A%22Advanced%20Search%22,%22modalId%22%3A%221670133126002%22,%22channelName%22%3A%22advanced-search-modal%22%7D%5D
Here is my code:
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('F:\\work\\chromedriver_win32\\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://www.upwork.com/nx/jobs/search/?sort=recency"
driver.get(url)
key = ["Web Scraping","Selenium WebDriver", "Data Scraping", "selenium", "Web Crawling", "Beautiful Soup", "Scrapy", "Data Extraction", "Automation"]
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#onetrust-accept-btn-handler')))
time.sleep(5)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#onetrust-accept-btn-handler'))).click()
for i in range(len(key)):
wait.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(#title,"Advanced Search")]'))).click()
time.sleep(5)
advanced_search_input = driver.find_element(By.XPATH,'//input[contains(#aria-labelledby,"tokenizer-label")]')
# advanced_search_input.click()
advanced_search_input.send_keys(key[i])
result giving now
By clicking '//input[contains(#aria-labelledby,"tokenizer-label")]' element it is re-built on the page (really strange approach they built that page).
To make this code working I added a delay after clearing and clicking that input and then get that element again.
The following code worked for me:
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, 10)
url = "https://www.upwork.com/nx/jobs/search/?sort=recency"
driver.get(url)
keys = ["Web Scraping","Selenium WebDriver", "Data Scraping", "selenium", "Web Crawling", "Beautiful Soup", "Scrapy", "Data Extraction", "Automation"]
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#onetrust-accept-btn-handler')))
time.sleep(5)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#onetrust-accept-btn-handler'))).click()
for i in range(len(keys)):
wait.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(#title,"Advanced Search")]'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,'//input[contains(#aria-labelledby,"tokenizer-label")]'))).clear()
wait.until(EC.element_to_be_clickable((By.XPATH, '//input[contains(#aria-labelledby,"tokenizer-label")]'))).click()
time.sleep(3)
wait.until(EC.element_to_be_clickable((By.XPATH, '//input[contains(#aria-labelledby,"tokenizer-label")]'))).send_keys(keys[i])
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'[data-test="modal-advanced-search-search-btn"]'))).click()
UPD
In order to select multiple search values you need to insert each value, select the appearing autocomplete option and continue, as in the code below:
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, 10)
url = "https://www.upwork.com/nx/jobs/search/?sort=recency"
driver.get(url)
keys = ["Web Scraping", "Selenium WebDriver", "Data Scraping", "Selenium", "Beautiful Soup", "Scrapy", "Data Extraction", "Automation"] #
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#onetrust-accept-btn-handler')))
time.sleep(5)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#onetrust-accept-btn-handler'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(#title,"Advanced Search")]'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,'//input[contains(#aria-labelledby,"tokenizer-label")]'))).clear()
wait.until(EC.element_to_be_clickable((By.XPATH, '//input[contains(#aria-labelledby,"tokenizer-label")]'))).click()
time.sleep(3)
for i in range(len(keys)):
wait.until(EC.element_to_be_clickable((By.XPATH, '//input[contains(#aria-labelledby,"tokenizer-label")]'))).send_keys(keys[i])
time.sleep(2)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#typeahead-input-control-35 .up-menu-item-text"))).click()
time.sleep(4)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'[data-test="modal-advanced-search-search-btn"]'))).click()
UPD
Finally did it!
The problem with wrong inputs caused by too slow response time of that page.
To make it working I inserted a small delay between inserting each character of the input string. In this case the result is as expected.
This is the final working code:
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, 10)
url = "https://www.upwork.com/nx/jobs/search/?sort=recency"
driver.get(url)
keys = ["Web Scraping", "Selenium Webdriver", "Data Scraping", "Selenium", "Beautiful Soup", "Scrapy", "Data Extraction", "Automation"]
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#onetrust-accept-btn-handler')))
time.sleep(5)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button#onetrust-accept-btn-handler'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, '//button[contains(#title,"Advanced Search")]'))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,'//input[contains(#aria-labelledby,"tokenizer-label")]'))).clear()
wait.until(EC.element_to_be_clickable((By.XPATH, '//input[contains(#aria-labelledby,"tokenizer-label")]'))).click()
time.sleep(3)
for i in range(len(keys)):
search_field = wait.until(EC.element_to_be_clickable((By.XPATH, '//input[contains(#aria-labelledby,"tokenizer-label")]')))
search_field.click()
for character in keys[i]:
search_field.send_keys(character)
time.sleep(0.05)
time.sleep(2)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#typeahead-input-control-35 .up-menu-item-text"))).click()
time.sleep(2)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,'[data-test="modal-advanced-search-search-btn"]'))).click()
The result is

I can't click a button with Selenium Python

My goal is to disable cookies when I access page https://www.icribis.com/it/ (that is click on the button "Rifiuta"). My code, which is not working, is:
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
import time
url = 'https://www.icribis.com/it/'
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)
time.sleep(5)
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[#id="uc-center-container"]/div[2]/div/div[1]/div/div[2]/button[2]'))).click()
time.sleep(5)
driver.close()
I found the XPath by inspecting the element on the web page.
How can I correct it?
It's in shadow-root.
You will have to use execute_script
url = 'https://www.icribis.com/it/'
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)
time.sleep(5)
cookie_dsbl_btn = driver.execute_script('return document.querySelector("#usercentrics-root").shadowRoot.querySelector("#uc-center-container > div:nth-child(2) div > button:nth-child(3)")')
cookie_dsbl_btn.click()
time.sleep(5)

How can I scroll down using selenium

The code is as below.
driver = webdriver.Chrome(chromedriver_path) #webdriver path
driver.get('https://webtoon.kakao.com/content/%EB%B0%94%EB%8B%88%EC%99%80-%EC%98%A4%EB%B9%A0%EB%93%A4/1781') #website access
time.sleep(2)
driver.execute_script("window.scrollTo(0, 900)") #scroll down
time.sleep(1)
However, the page does not scroll.
How can I scroll?
Page link to be scrolled
Try this
driver.execute_script("window.scrollTo(100,document.body.scrollHeight);")
Tried with the below code, it did scroll.
driver.get("https://webtoon.kakao.com/content/%EB%B0%94%EB%8B%88%EC%99%80-%EC%98%A4%EB%B9%A0%EB%93%A4/1781")
time.sleep(2)
options = driver.find_element_by_xpath("//div[#id='root']/main/div/div/div/div[1]/div[3]/div/div/div[1]/div/div[2]/div/div[1]/div/div/div/div")
driver.execute_script("arguments[0].scrollIntoView(true);",options)
The webapp is dynamic (which is, the more you scroll down, more you will see the data), you can perform infinite scrolling like below :-
driver = webdriver.Chrome(chromedriver_path) #webdriver path
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://webtoon.kakao.com/content/%EB%B0%94%EB%8B%88%EC%99%80-%EC%98%A4%EB%B9%A0%EB%93%A4/1781') #website access
time.sleep(2)
wait = WebDriverWait(driver, 20)
time.sleep(5)
action = ActionChains(driver)
i = 3
while True :
action.move_to_element(driver.find_element(By.XPATH, f"(//div[contains(#class, 'AspectRatioBox_aspectRatioBox')])[{i}]")).perform()
i = i +1
time.sleep(.5)
Imports :
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.webdriver.common.action_chains import ActionChains

How to click recaptchaV2's Solve the challenge button using Selenium and Python

I'm trying to interact with the recaptchaV2 Solve the challenge button on image verification popup using Selenium and Python.But meet some problem.By the way,I use buster chrome extension to bypass the recaptcha.Hope can help me.Thank you~
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = webdriver.ChromeOptions()
chrome_options.add_extension('~/Library/Application Support/Google/Chrome/Default/Extensions/mpbjkejclgfgadiemmefgebjfooflfhl/1.1.0_0.crx')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.google.com/recaptcha/api2/demo")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.google.com/recaptcha/api2/anchor']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#recaptcha-anchor"))).click()
driver.switch_to.default_content()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#solver-button"))).click()
problem like
chrome_options = webdriver.ChromeOptions()
Is outdated use Options.
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
Also the audio button is
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#recaptcha-audio-button"))).click()
Not
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#solver-button"))).click()
It also detects automation so use
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
Evil company was change class name in [challenge]
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge expires in two minutes']")))
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.get('https://www.google.com/recaptcha/api2/demo')
element_present = driver.find_element(By.XPATH, '//*[#title="reCAPTCHA"]')
driver.switch_to.frame(element_present)
element_present = EC.presence_of_element_located((By.XPATH, '//*[#id="recaptcha-anchor"]/div[1]'))
WebDriverWait(driver, 15, poll_frequency=POLL_FREQUENCY).until(element_present).click()
driver.switch_to.default_content()
time.sleep(5)
element_present = driver.find_element(By.XPATH, "/html/body/div[2]/div[4]/iframe")
driver.switch_to.frame(element_present)
try:
buttonHolderElement = driver.find_element(By.XPATH, '//*[#id="rc-imageselect"]/div[3]/div[2]/div[1]/div[1]/div[4]')
actions = ActionChains(driver)
actions.move_to_element(buttonHolderElement)
actions.click(buttonHolderElement)
actions.perform()
except:
pass

Categories