I need to click on the "Visualizar cruzeiros" button on this site: https://www.disneytravelcenter.com/MIN-000000000031063/sites++disney-cruise-line++cruises-destinations++alaska++view-sailings++view-sailings/
Unfortunately, I cannot do it by simply using:
visualizar = driver.find_element_by_css_selector('#jb-card-txt-blk-redirect-link-zone-' + zona + 'night-' + str(c)+ '.redirectLink')
visualizar.click()
Or using:
visualizar= driver.find_element_by_class_name('redirectLink')
visualizar.click()
Or using:
visualizar= driver.find_element_by_xpath('//*[#id="jb-card-txt-blk-redirect-link-zone-' + zona + 'night-' + str(c) +'"]')
visualizar.click()
How am I supposed to click on this button then?
OBS: The "zona" and str(c) are loop variables, but they don't change the result of the code, actually.
Have you tried using xpath //a[contains(text(),'Visualizar cruzeiros')? This should return more than one element, you need to get the second one.
Try to write a test case with selenium IDE, it may give you a hint how to access the element.
Found out the answer to my problem:
button = driver.find_elements_by_xpath('//*[#id="jb-card-txt-blk-redirect-link-zone-' + zona + 'night-' + str(c) + '"]')[2]
button.click()
Related
I am working on web automation with selenium and I need to find an element with the xpath. This by itself is not a problem but the code needs to run multiple times and when it does that the HTML xpath changes. This is also not a problem. I have used simple math to get the new xpath every time.
The xpathes look like this
1. run: '//*[#id="input-text-3"]'
2. run: '//*[#id="input-text-5"]'
3. run: '//*[#id="input-text-7"]' etc.
I solved this problem using this code:
y = 1
#Corme browser already defined and on website
while True:
mathop1 = y*2 + 1
xxpath = ""'//*[#id="input-text-' + str(mathop1) + '"]'""
xxpath1 = "'" + str(xxpath) + "'"
print(xxpath1)
Bezeichnung = driver.find_element_by_xpath(xxpath1)
Bezeichnung.send_keys(file1name)
y = y + 1
What this does is that every time the program loops it updates y so the xpath will be correct. I tried using the output from xxpath1 to find the element like you normally would and it works fine however as soon as I use the variable it does not work. Specifically, the problem is this I can't use the variable.
Bezeichnung = driver.find_element_by_xpath(xxpath1)
why does this not work?
First of all, I guess you have to put a wait condition there.
Also, I do not understand why are you using so much strings inside string and converting string to string again, so I removed that
y = 1
#Corme browser already defined and on website
while True:
mathop1 = y*2 + 1
xxpath = '//*[#id="input-text-{}"]'.format(mathop1)
Bezeichnung = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, xxpath)))
Bezeichnung.send_keys(file1name)
y = y + 1
The address below is the xpath address.
//label[#for="check54490"])
The check box is checked when typed as below.
driver.find_element_by_xpath('//label[#for="check54490"]').click()
However, there are many check boxes that need to be checked, so I am going to make variables and check them.
I made a variable as below, but an error occurs.
umber = [54490]
for num in umber:
elem = f'//label[#for="check{num}"]'
elm2 = f"'{elem}'"
xpath = elem2
driver.find_element_by_xpath(xpath).click()
the error message is as follows.
Is the approach good or is it impossible in the first place?
Please give me some advice.
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression driver.find_element_by_xpath('//label[#for="check1000054759"]').click() because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string 'driver.find_element_by_xpath('//label[#for="check54490"]').click()' is not a valid XPath expression.
(Session info: chrome=90.0.4430.85)
Instead of doing it in for loop you can directly do it in the xpath for single value as below -
driver.find_element_by_xpath("//label[#for='check" + str(umber[0]) + "']")
Now since you have asked how you can do it in case of multiple values consider the following code example -
umber = [54490, 54490]
for num in umber:
driver.find_element_by_xpath("//label[#for='check" + str(num) + "']").click()
OR you can have each number in different variable -
xbuttonNum = '54490'
ybuttonNum = '54491'
zbuttonNum = '54521'
driver.find_element_by_xpath("//label[#for='check" + xbuttonNum + "']").click()
driver.find_element_by_xpath("//label[#for='check" + ybuttonNum + "']").click()
driver.find_element_by_xpath("//label[#for='check" + zbuttonNum + "']").click()
Alternatively you can customize your xpath unique for all the elements and iterate over them all at once like -
AllElementwithCheckString = driver.find_elements_by_xpath("//label[starts-with(#for,'check')]")
for elem in AllElementwithCheckString:
elem.click()
Relatively new in programming, doing Python with Selenium and Xpath
this is from this website:https://www.marketbeat.com/stocks/NASDAQ/AMZN/earnings/
what I am trying to do is copy the dates of the past earnings.
Problem is it is in the table and it also has tag.
I have this full Xpath:
/html/body/div[1]/main/article/form/div[8]/div[2]/div[2]/div/div[5]/div/table/tbody/tr[2]/td[1]
and upon inspection it shows this
< td data-sort-value="20201027000000">10/27/2020 < / td >
I am trying to get the 10/27/2020 out from here but I don't know how to.
It is easy when it is just the < td >$1.53< /td > where just giving the full xpath and then doing a .text on it gives me the text.
point is how do I get 10/27/2020. I would suspect that I would have to go over data-sort-value="" part.
Here is what I have :
stringeEPSxpath = """/html/body/div[1]/main/article/form/div[8]/div[2]/div[2]/div/div[5]/div/table/tbody/tr[""" + row + """]/td[1]"""
Date = stringeEPSxpath.text
(the row part is just me iterating the website page through a loop no problem there.)
For the rest of my code it has been pretty simple :
stringeEPSxpath = """/html/body/div[1]/main/article/form/div[8]/div[2]/div[2]/div/div[5]/div/table/tbody/tr[""" + row + """]/td[3]"""
stringEPSxpath = """/html/body/div[1]/main/article/form/div[8]/div[2]/div[2]/div/div[5]/div/table/tbody/tr[""" + row + """]/td[4]"""
elem = driver.find_element_by_xpath(stringeEPSxpath)
elem2 = driver.find_element_by_xpath(stringEPSxpath)
fix1 = (elem.text).replace(')','')
fix2 = (elem2.text).replace(')','')
eps1 = fix1.replace('(','-')
eps2 = fix2.replace('(','-')
As you can see above all I had to do was set a variable to it then use .text to convert it to a string. But now that does not work for dates.
The end result is the error of AttributeError: 'str' object has no attribute 'text' which I would suspect is because of the data-sort-value="20201027000000" part.
In this code stringeEPSxpath is string, not WebElement.
stringeEPSxpath = """/html/body/div[1]/main/article/form/div[8]/div[2]/div[2]/div/div[5]/div/table/tbody/tr[""" + row + """]/td[1]"""
Date = stringeEPSxpath.text
You can try this one:
from selenium.webdriver.common.by import By
element = driver.find_element(By.XPATH,"//td[#data-sort-value='20201027000000']")
Date = element.text
Forgot driver.find_element_by_xpath(stringeEPSxpath) which actually uses xpath. Since I copy and pasted from my other program I guess I deleted too much and completely forgot to add in the driver.findxpath.
I'm trying to insert the variable "i" on my Xpath but I'm hitting a wall.
This is my code:
for i in range(len(results_nbr)):
xpath = "//*[#id='w1']/div["
xpath += int(i)
xpath += "]/div/div[2]/h3"
elem = driver.find_element_by_xpath(xpath)
elem.click()
time.sleep(1)
I'm using selenium and it works fine if I have the variable in the code as below:
elem = driver.find_element_by_xpath('//*[#id="w1"]/div[1]/div/div[2]/h3')
What is wrong with my code please? I tried int(i) and str(i) with same result.
I appreciate your help. Thanks
First point: use string formatting. There are a lot of available options in Python:
- xpath = '//*[#id="w1"]/div[%s]/div/div[2]/h3' % i
- xpath = '//*[#id="w1"]/div[{}]/div/div[2]/h3'.format(i)
- xpath = f'//*[#id="w1"]/div[{i}]/div/div[2]/h3' # in Python 3.6 and later
Second point: in XPath indexing starts from 1. So div[0] won't work. Try
for i in range(1, len(results_nbr) + 1):
...
I am using the python3 unittest library with selenium webdriver. I have the exisiting already question in my application. I am trying to add another question but when I am trying to insert text into text field in my new question - it changes text in my existing question instead of the new one...
I am using:
wd.find_element_by_id("id_question-1-title").click()
wd.find_element_by_id("id_question-1-title").clear()
wd.find_element_by_id("id_question-1-title").send_keys("ABC")
But this solution requires changing id everytime I run code into id_question-2-title, id_question-3-title etc
You can check how many questions you have and give the index accordingly
questions = wd.find_elements_by_css_selector('[id*="id_question-1-title"]')
index = len(questions)
wd.find_element_by_id('id_question-' + index + '-title').click()
wd.find_element_by_id('id_question-' + index + '-title').clear()
wd.find_element_by_id('id_question-' + index + '-title').send_keys("ABC")
Guy, thank you very much! I just added str to len(questions) because it required str insted of int
questions = wd.find_elements_by_css_selector('[id*="id_question-1-title"]')
index = str(len(questions))
wd.find_element_by_id('id_question-' + index + '-title').click()
wd.find_element_by_id('id_question-' + index + '-title').clear()
wd.find_element_by_id('id_question-' + index + '-title').send_keys("ABC")