I'm having a big problem with this one and I just don't know what to do now.
So, I'm trying to use selenium to get an information on a sort of a pop up.pop up
(This is this exact pop-up, it's on tiktok)
The HTML element of the button followers : Followers<
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
from selenium.common.exceptions import TimeoutException
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/#programm___r?lang=en") # on lance tiktok sur ordi
# first thing first, just click on the button that "launch" the pop up
driver.find_element_by_class_name('jsx-1737374796.header-inbox-icon').click()
# Then, I want to click on "Followers" category but this is getting complicated here
# First idea to click on the category, check if it contains the word "followers"
if (driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').text) == "Followers" : # this line works, no problem
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').click() # this one doesn't work i don't know why
# So, second idea, try with the webdriverwait
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]'))) # this works
driver.find_element_by_xpath('/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]').click() # this still doesn't work
# Third idea, instead o xpath, css-selector
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.CSS_SELECTOR,"span.jsx-2344352055:nth-child(5)"))) # work, no problem
driver.find_element_by_css_selector("span.jsx-2344352055:nth-child(5)").click() # doesn't work neither..
# Fourth and last idea, probably the least fine, get all the elements with the class name, and only one return Followers with the .text, but still the same problem
elements = (driver.find_elements_by_class_name("jsx-2344352055"))
for i in range(len(elements)) :
if elements[i].text == "Followers" :
elements[i].click() # but still doesn't work
Sometimes, it worked, like I don't know why or how but sometimes, the click works, but like 95% of the time, it doesn't and I really don't know why
Thanks in advance for your help !
You should wait for the button to be clickable using WebDriverWait and element_to_be_clickable expected condition. For example:
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
from selenium.common.exceptions import TimeoutException
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/#programm___r?lang=en")
WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]')))
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[1]/div/div[1]/div/div[3]/div[2]/div[2]/div/div[1]/div/span[5]'))).click()
Please try this one:
driver = webdriver.Firefox(executable_path='./geckodriver')
driver.get("https://www.tiktok.com/#programm___r?lang=en") # on lance tiktok sur ordi
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS) ;
# first thing first, just click on the button that "launch" the pop up
driver.find_element_by_class_name('jsx-1737374796.header-inbox-icon').click()
I hope this resolves your issue.
Related
Can someone help me please, I have been stuck in one place in my script for 2 days .
A menu should be clicked and an option should be selected, unfortunately it does not work, I have started about 50 code attempts, unfortunately without success
This is the html text for which I need a Selenium code solution
<select class="categoryAttributesSelect form-control"><option value="-1">Bitte Zustand auswählen...</option><option value="22">Neu</option><option value="2546">Neuwertig</option><option value="23">Gebraucht</option><option value="24">Defekt</option></select>
I need a code that selects the option (new) for me
This not Working..
minimenu1 = Select(browser.find_element_by_xpath('/html/body/div[4]/div/div/form/div[1]/div[1]/div[2]/div[2]/div[3]/div[2]/div[4]/div[1]/div[2]/select'))
minimenu1.select_by_visible_text("Neu")
minimenu1.select_by_value("22")
See if this works.
minimenu1 = Select(browser.find_element_by_xpath("//select[contains(#class,'categoryAttributesSelect')]"))
minimenu1.select_by_visible_text("Neu")
You can use the below code :
wait = WebDriverWait(driver, 10)
menu = Select(wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "select.categoryAttributesSelect.form-control"))))
menu.select_by_visible_text('Neu')
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Python/coding newb, beware!
Im writing a script to download from youtube from " https://ytmp3.cc/en13/ ". I had written a click script using pyautogui but the problem being that the download button appears anywhere betweeen 1 and 15 seconds of entering the link. So i wanted to re-code it in a Selenium window to dynamically wait until the button is visible and then continue with the click script. I have tried about 15 ways but cant get it to work
library blah :
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
ways i tried to get it to work:
without waits, results in NosuchElement Exception
def check_exists():
try:
while True:
a=driver.find_element(By.LINK_TEXT, "Download")
a.click()
except NoSuchElementException:
time.sleep(10)
the syntax of this is most probably wrong
i tried a couple different variations of implicit and explicit waits but couldnt get it to work. Is there some javascript on the site linked above thats messing with my code?
in a separate .py, running
#driver.find_element(By.LINK_TEXT, "Download").click()
#either the line above or below works at finding the element, and clicking it
driver.find_element_by_link_text("Download").click()
I just need help getting the line above ^ into an explicit wait, but i dont have the language knowledge of how to do it yet. i would appreciate some help, thanks in advance!
You can use webdriver wait to wait until button is displayed. Tried the below code which works for me
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
video_path = "your_video_path"
waitTime=10
driver = webdriver.Chrome("./chromedriver.exe")
driver.get("https://ytmp3.cc/")
element = driver.find_element_by_id("input")
element.send_keys(video_path)
driver.find_element_by_id("submit").click()
try:
# wait 10 seconds before looking for element
element = WebDriverWait(driver, waitTime).until(
EC.presence_of_element_located((By.LINK_TEXT,"Download"))
)
driver.find_element_by_link_text("Download").click()
finally:
print(driver.title)
You can update your required wait time in waitTime variable in seconds so if the value is 10 selenium will wait and check for elements upto 10 seconds
I am trying out Selenium for the first time so I apologize if there is an obvious mistake or problem with my code.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://youtube.com')
searchBox = driver.find_element_by_id('search')
searchBox.send_keys('Programming')
searchButton = driver.find_element_by_id('search-icon-legacy')
searchButton.click()
So I tried this and it loads the page fine but, it does not input any characters into the searchBox (I quadruple checked that the id was correct - copied it directly from the inspector).
NOTE:
My internet is really REALLY slow and it takes YouTube approx. 20 seconds to fully load, so I thought that was an issue so I tried;
...
driver.get('https://youtube.com')
driver.implicitly_wait(30)
searchBox = driver.find_element_by_id('search')
...
But this did not work either.
I did use XPATH instead of finding it by element ID at the start and that did not work.
I checked and copied the XPATHs and IDs directly from the inspector and nothing so far has inputted anything into the textbox.
What could be the problem? (1)
and does the webdriver wait for the page to load/find the element before doing anything after it being initialized with the driver.get('websiteAddress')? (2)
NOTE: I double checked that I was selecting the right element as well.
To send keys to the input tag with id = search. We use webdriver waits to allow the element to be usable after driver.get so the page loads correctly.
driver.get('https://youtube.com')
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='search']"))).send_keys("Programming")
Import
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
If you don't know the waiting time:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
delay = 40
WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.XPATH, "//form[#role='form']//input[#id='username']")))
Then it just waits on the element, for as log as delay is, but will continue as soon as the element is found, that the best way to wait on slow connections.
To relate elements more easily you can use ChroPath, it is an extension for google chrome / edge that allows you to see the path of an element, through cssSelector, Abs XPath, Rel XPath and the tag name, so when your code is not working you can try these other ways. Particularly this extension helps me a lot.
I am Trying to create a bot that can fill a cart with these beer bottles. I really want to do this on a few different sites but for some reason i can only get it to open the page and click the first button, then it doesn't click the next button. I tried ID, Name, pretty much anyway to identify the button but it won't click it. I even tried sleep for 3 seconds. I tried to see if it was in an Iframe but i don't think it is. I'm out of ideas.... Link is https://www.sideprojectbrewing.com/shop?category=Beer+Release
I'm trying to access the add to cart element but does not seem to work
\
from Config import keys
from selenium import webdriver
def order(k):
driver = webdriver.Chrome(executable_path=r"C:\Users\ezliv\Desktop\ShopBot1\chromedriver_win32\chromedriver.exe")
driver.get(k['product_url'])
driver.find_element_by_xpath('//*[#id="thumb-biereblanche"]/div/div[1]/div/div/img').click()
driver.find_element_by_xpath('//*[#id="yui_3_17_2_1_1606181545139_755"]').click()
\\
You can try following code:
driver.get(url_here)
wait = WebDriverWait(driver, 20)
bottle = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="thumb-biereblanche"]/div/div[1]/div/div/img')))
bottle.click()
add_to_cart = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'sqs-add-to-cart-button-inner')))
add_to_cart.click()
Import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I'm trying out the selenium framework to learn something about the web browser automation. Therefore, I decided to build an Audi model...
My code so far:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
chrome_driver = webdriver.Chrome(executable_path=r"chromedriver_win32\chromedriver.exe")
chrome_driver.get("https://www.audi.de/de/brand/de/neuwagen.html")
# choose A3 model
chrome_driver.find_element_by_xpath('//*[#id="list"]/div[1]/ul/li[2]/a[2]').click()
# choose sedan version
WebDriverWait(chrome_driver, 3).until(EC.visibility_of_element_located((By.XPATH, '//*[#id="a3limo"]/div/a'))).click()
# start model configuration
WebDriverWait(chrome_driver, 3).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[1]/div[2]/div/div[6]/div[2]/div/div[1]/ul/li[1]/a'))).click()
# choose the s-line competition package
WebDriverWait(chrome_driver, 3).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[1]/div[2]/div/div[7]/div[2]/div[2]/div[3]/div[1]/div/div[1]/div/div[1]/span/span'))).click()
# accept the s-line competition package
WebDriverWait(chrome_driver, 3).until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[5]/div/div/div/div/div/ul[2]/li[2]/a'))).click()
Now, the code fail already on the line start model configuration (see "Konfiguration starten" button on webpage https://www.audi.de/de/brand/de/neuwagen/a3/a3-limousine-2019.html). The xPath must be correct and the element should also be visible, so what am I doing wrong here?
Actually required link becomes visible only if to scroll page down.
Try to use this piece of code to click link:
configuration_start = chrome_driver.find_element_by_xpath('//a[#title="Konfiguration starten"]')
chrome_driver.execute_script('arguments[0].scrollIntoView();', configuration_start)
configuration_start.click()
As navigation panel has fixed position it might overlap target link, so you can change navigation panel style before handling link:
nav_panel = chrome_driver.find_element_by_xpath('//div[#data-module="main-navigation"]')
driver.execute_script('arguments[0].style.position = "absolute";', nav_panel)
The following seems to work for me. I have added waits for elements and used a simulated click of the element via javascript.
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
url = 'https://www.audi.de/de/brand/de/neuwagen.html'
d = webdriver.Chrome()
d.get(url)
WebDriverWait(d,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '[data-filterval="a3"]'))).click()
d.get(WebDriverWait(d,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#a3limo .mf-model-details a'))).get_attribute('href'))
element = WebDriverWait(d,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '[title="Konfiguration starten"]')))
d.execute_script("arguments[0].click();", element)