My for loop goes to the latest part, and because the webpage needs the upper for loops to open so that the other for loops can operate, it doesn't work.
for x in browser.find_elements_by_class_name('node'):
x.click()
for y in browser.find_elements_by_class_name('dTreeNode'):
y.click()
if len(browser.find_elements_by_class_name('node')) > 0:
browser.find_element_by_class_name('node').click()
browser.find_element_by_xpath('/html/body/center[2]/form/table[1]/tbody/tr/td[3]/table/tbody/tr[5]/td[1]/a[1]/img').click()
browser.find_element_by_xpath('/html/body/center[2]/form/table[2]/tbody/tr/td[4]/input').click()
browser.find_element_by_xpath('/html/body/center/form/table[2]/tbody/tr/td[5]/a').click()
browser.execute_script("window.history.go(-1)")
This is the image:
In essence when the page goes back, it can't locate the last loop class and shows up an error because the element can only be found once I click on the previous element. But how do I do that inside the for loop?
Image of the HTML Text:
It seems that either the for y in browser.find_elements_by_class_name('dTreeNode'): returns an empty element or click is performed on a wrong element i.e. element is not programmed to receive the click.
Either case -
First thing - Please check if your first loop executing the code or not.
for y in browser.find_elements_by_class_name('dTreeNode'):
y.click()
print('Clicked the element') #just for debugging
Then see if the element is returned or not.
Secondly - I see you are using absolute xPath. Can you convert it to relative xPath. Refer to XPath Tutorials
If you need help on xPath, update your question and HTML snippet of the expanded tree.
Related
I'm struggling to make code like:
click the button with the help of the selenium unless a new table get created and stop after the table exists. I'm using python with selenium.
i've tried to search everywhere but didn't find the solution. any help will be appreciated.
my code
while not driver.find_element(By.ID("tableID")):
submit_btn.click()
Change find_element with find_elements.
find_element will throw exception in case of no element exists while find_elements returns a list of web elements matching the passed locator. In case of matching elements existing it will return a non-empty list interpreted by Python as a Boolean True. Otherwise it will return an empty list interpreted by Python as a Boolean False.
Also you have a syntax problem in (By.ID("tableID")).
This should work:
while not driver.find_elements(By.ID, "tableID"):
submit_btn.click()
Also you may wish to put some delay inside the loop to not perform clicks too fast, so this may be better:
while not driver.find_elements(By.ID, "tableID"):
submit_btn.click()
time.sleep(0.1)
I'm having some trouble finding elements with Selenium in Python, it works fine for every element on all other websites I have tested yet on a game website it can only find certain elements.
Here is the code I'm using:
from selenium import webdriver
import time
driver = webdriver.Chrome("./chromedriver")
driver.get("https://www.jklm.fun")
passSelf = input("Press enter when in game...")
time.sleep(1)
syllable = driver.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/div[2]/div").text
print(syllable)
Upon running the code, the element /html/body/div[2]/div[2]/div[2]/div[2]/div isn't found. In the image you can see the element it is trying to find:
Element the code is trying to find
However running the same code but replacing the XPath with something outside of the main game (for example the room code in the top right) it successfully finds the element:
Output of the code being run on a different element
I've tried using the class name, name, selector and XPath to find the original element but no prevail the only things I can think that are affecting it is that:
The elements are changing periodically (not sure if this affects it)
The elements are in the "Canvas area" and it is somehow blocking it.
I'm not certain whether these things matter as I'm new to using selenium any help is appreciated. The website the game is on is https://www.jklm.fun/ if you want to have a look through the elements
Element you are trying to access is inside an iframe. Switch to the frame first like this
driver.switch_to_frame(driver.find_element_by_xpath("//div[#class='game']/iframe[contains(#src,'jklm.fun')]"))
driver.get("https://jklm.fun/JXUS")
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//button[#class='styled']"))).click()
time.sleep(10)
driver.switch_to.frame(0)
while True:
Get_Text = driver.find_element_by_xpath("//div[#class='round']").text
print(Get_Text)
I am trying to apply find_element_by_xpath only on specific classnames
textboxes = driver.find_elements_by_class_name("freebirdFormviewerViewNumberedItemContainer")
for element in textboxes:
a = element.find_element_by_xpath("//*[contains(#id, 'i')]")
I want to find the element by x path that contains the first item that has "id" in it, as there could be multiples in that node e.g "id1", "id2"
The problem is that it keeps returning the same id i.e "id1" despite me using the for loop.
Does it not loop through different element in the textboxes list? I want to apply the find_element_by_xpath only under that child node.
You are close to the right solution but missing a small point . :)
So inside the for loop you should use a = element.find_element_by_xpath(".//*[contains(#id, 'i')]")
I mean the . before the // at the beginning of the xpath expression.
Also possibly there is a problem with i inside the contains(#id, 'i') - I don't see where you are updating it's value.
To find the input element you can simply use the following css selector .freebirdFormviewerViewNumberedItemContainer input or if you prefer xpath you can use //div[#class='freebirdFormviewerViewNumberedItemContainer']//input
You are not augmenting i inside your loop, so it just stays the same for every iteration.
I'm trying click on elements all elements with the link text edit by:
for i in browser.find_elements_by_link_text('edit'):
browser.find_element_by_link_text('edit').click()
Now, this obviously only clicks on the first element found i times. I would usually try to use XPath/CSS Selector and loop using an index as you do with tables to get the next element, but these XPaths are identical other than having different ID's, so I don't know how to click on all elements in this case.
It's Reddit comments I try to edit:
With the XPaths:
//*[#id="e9sczhm"]/div[2]/div[2]/a[5]
//*[#id="e9s7x4j"]/div[2]/div[2]/a[5]
it should be i.click()
for i in browser.find_elements_by_link_text('edit'):
i.click()
for i in browser.find_elements_by_link_text('edit'):
browser.find_element_by_link_text('edit').click()
to
from selenium.webdriver.common.keys import Keys
# Find just one element
browser.find_element_by_link_text('edit').click() #<<<--- Starting point (cursor current position)
scheme = (Keys.TAB,Keys.Enter,Keys.TAB,Keys.TAB,Keys.Enter)
for i in scheme : browser.sendKeys(i)
[Starting pos/elem][TAB][HIT][Element-1][TAB][Element-2][TAB][Element-3][TAB][HIT][Element-4]
just hit first and fourth element !
I'm new to Python.
I met a BIG problem in Python!!
I visit a website and put about 200 options from a dropdownlist in a array.
I want to click every options in the array and click javascript button to submit.
Take something I want from that page and back to previous page click another option.
Do those actions about 200 times in a for loop.
Here is the code:
for option in arrName:
if count > 0:
option.click()
string = u'Something'
link2 = browser.find_element_by_link_text(string.encode('utf8'))
link2.click()
//"do something I want"
browser.back()
count = count +1
In this code, I don't want to use first option.
PROBLEM comes,after the program click the second option, click the link2, and browser.back(), it answer me:
` StaleElementReferenceException: Message: stale element reference: element
is not attached to the page document
that means the options in array disappear?
How should I use the options in the array when the browser.back() in for loop?
Thanks
Yes, this is happening because of DOM refresh. You cannot simply iterate over an array and click back and forth. Best option is to find the element in runtime and then click. Avoid option.click() instead, find the next element with find_element. If you are not sure how to accomplish that then please provide the html